vrijdag 2 december 2011

Powershell & SharePoint

Yesterday i received an interesting blog item from my colleage which i wanted to share with you.

Hi All,

If you are thinking about creating a SharePoint environment with the help of our friend PowerShell, you will discover that almost everything is possible.

So let's create a  'User' (person/group) column :

### Add custom site column ###

$fieldXML = ' Name="myUser"
Description="name it"
Type=”User”
DisplayName="Add me to your list ..."
Group="Custom Columns"
Required="true"
ShowInDisplayForm="true"
ShowInEditForm="true"
ShowInListSettings="true"
ShowInNewForm="true"
List="UserInfo"
ShowField="ImnName"
UserSelectionMode = "PeopleOnly"
UserSelectionScope = "0">
'
$spWeb.Fields.AddFieldAsXml($fieldXML)

$spWeb.Update()

The column will appear in the 'Custom Columns'


Next thing, we will add this column to a content type :

### Add column to CT ###

$spColumn = New-Object Microsoft.SharePoint.SPFieldLink ($spWeb.Fields["Name"])

$Ct = $spWeb.ContentTypes["ContentType"]

$Ct.FieldLinks.Add($spColumn)

$Ct.Update()

Your site column was added to your content type ...


Enjoy PowerShell :-)