2

I want to create a site column with preferred display name and internal name under a particular group.

I gone through these sites

http://www.c-sharpcorner.com/UploadFile/54db21/site-column-in-sharepoint-2010-programmatically-using-vs-201/

http://www.sharepointdiary.com/2012/08/how-to-add-site-column-programmatically.html

Is there a way to achieve what i want using CSOM?

3

You can try below code sample:

 ClientContext context = new ClientContext("http://SiteURL");
 FieldCollection fieldCollection = context.Web.Fields;
 fieldCollection.AddFieldAsXml(@"<Field ID='{4AA6616E-E435-4B52-A30B-54B7D3EE6260}' Type='Text'
                                            Name='InternalName'
                                            Required='TRUE'
                                            DisplayName='DisplayName'
                                            Description=''
                                            Group='GroupName'/>",true, AddFieldOptions.DefaultValue);
 context.ExecuteQuery();
1

If you want to use CSOM with PowerShell to create Site Columns, then refer following code.

Connect to SharePoint Site, and load all Site Columns

$url ="http://YourSite"
# site columns
$Name ="YourSiteColumnInternalName"
Write-Host "authenticate to SharePoint site collection $url and get ClientContext object" -foregroundcolor black -backgroundcolor yellow  
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = Get-Credential 
$Context.Credentials = $credentials 
$web = $context.Web
$fields = $web.Fields;         
$site = $context.Site 
$context.Load($web)
$context.Load($fields)
$context.Load($site)
try
{
    $context.ExecuteQuery()
    Write-Host "authenticateed to SharePoint site collection $url and get ClientContext object succeefully" -foregroundcolor black -backgroundcolor Green
}
catch
{
    Write-Host "Not able to authenticateed to SharePoint site collection $url $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
    return
}

Check if Site Column already exists in collection

Write-Host "Check if the site column" $Name "is exist or not" -foregroundcolor black -backgroundcolor yellow  
foreach ($item in $fields)
{
    if ($item.InternalName -eq $Name)
    {
        Write-Host "Site column" $Name "is already exist" -foregroundcolor black -backgroundcolor Red
        return
    }
}

If your site column do not exists, then add it in the Site Column collections

Write-Host "creating site column" $Name -foregroundcolor black -backgroundcolor yellow 

$fieldAsXML = 
            "<Field Type='Text' 
            DisplayName='Your Display Name'
            Name='"+$Name+"'
            ID='{545E5A0D-5553-4E02-97B3-F2D225B649D7}'
            Group='Your Site Columns' />";

$fieldOption = [Microsoft.SharePoint.Client.AddFieldOptions]::DefaultValue

$field = $fields.AddFieldAsXml($fieldAsXML, $true, $fieldOption)
$context.Load($field)
try
{
    $context.ExecuteQuery()
    Write-Host "Site column" $Name "created successfully" -foregroundcolor black -backgroundcolor Green 
}
catch
{
    Write-Host "Error while creating site column" $Name $_.Exception.Message -foregroundcolor black -backgroundcolor Red 
    return
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.