Create List and respective fields programmatically Office 365 SharePoint 2013

Hi Friends, Create List and respective fields programmatically CSOM and PowerShell scripting. CSOM and PowerShell scripting has become my favorite nowadays for office 365 projects. I will be sharing code to create lists and respective fields below.

Ps Script –
cls

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$xmlFilePath = $(“$dp0\Inputs.xml”)
$xmldata = [xml](Get-Content($xmlFilePath));

$username = “samtest@microsoft.com”
$password = Read-Host -Prompt “Please enter your password” -AsSecureString

$url = $xmldata.WebSite.Url

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials

Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Publishing.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Taxonomy.dll”

if (!$clientContext.ServerObjectIsNull.Value)
{
Write-Host “Connected to SharePoint Online site: ‘$Url'” -ForegroundColor Green

$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()

}

function createListFields([Microsoft.SharePoint.Client.Web] $web)
{

foreach($list in $xmldata.WebSite.Lists.List)
{
$isActive=$list.Active;
if($isActive -eq “True”)
{

$listName=$list.Name
$listCollection=$web.Lists;
$clientContext.Load($listCollection)
$clientContext.ExecuteQuery()

$spList = $listCollection | where{$_.Title -eq $listName}

if(!$spList)
{

$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = $listName
$ListInfo.Url=$list.Url
$ListInfo.TemplateType = $list.TemplateId

$spList = $web.Lists.Add($ListInfo)
$spList.Description = $ListTitle
$spList.Update()

}

$clientContext.Load($spList)
$clientContext.ExecuteQuery()

$fieldCollection=$spList.Fields;

foreach ($node in $list.Field) {

$fieldCollection.AddFieldAsXml($node.OuterXml,$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint);
$clientContext.Load($fieldCollection)
$clientContext.ExecuteQuery()
}

$spList.Update()
}
}
Write-Host “Lists and Fields created”

}

createListFields $web



XML Inputs-
<?xml version=”1.0″ encoding=”utf-8″?>
<WebSite Url=”https://microsft.sharepoint.com/sites/test/” >
<Lists>
<List Name=”Post Idea” TemplateId=”100″ Active=”True” Url=”Lists/PostIdea”>
<Field ID=”{46ECDE55-6BB0-4E57-A7EB-5FE9D0F4FF59}”
Name=”IdeaOwner”
DisplayName=”Idea Owner”
Type=”User” UserSelectionMode=”PeopleOnly”
Required=”False”
StaticName=”IdeaOwner”/>
<Field ID=”{B1B21CD8-2E46-4708-82C2-249ADFAFF566}”
Type=”Text” DisplayName=”Idea Description” Required=”False” MaxLength=”255″ StaticName=”IdeaDescription” Name=”IdeaDescription” />
<Field ID=”{6C78A432-7314-4112-AF38-C4FD80DE44BB}”
Name=”IdeaType”
DisplayName=”Idea Type”
Type=”Choice” StaticName=”IdeaType”
Required=”FALSE” FillInChoice=”TRUE”>
<Default>Lighting-Service</Default>
<CHOICES>
<CHOICE>Lighting-Service</CHOICE>
<CHOICE>Light for health and living</CHOICE>
</CHOICES>
</Field>
<Field ID=”{0B02FCE9-0C5F-40C7-AF15-E354BF61021C}”
Name=”IdeaCategory”
DisplayName=”Idea Category”
Type=”Choice”
Required=”FALSE” StaticName=”IdeaCategory”>
<Default>Healthcare</Default>
<CHOICES>
<CHOICE>Healthcare</CHOICE>
<CHOICE>Lifestyle</CHOICE>
<CHOICE>Lighting</CHOICE>
</CHOICES>
</Field>

<Field ID=”{C819B243-4C0B-4429-B62B-AC98E013FC4F}”
Name=”StrategicFit”
DisplayName=”Strategic Fit”
Type=”Choice”
Required=”FALSE” StaticName=”StrategicFit”>
<Default>Solution</Default>
<CHOICES>
<CHOICE>Solution</CHOICE>
<CHOICE>Device</CHOICE>
<CHOICE>Health Care</CHOICE>
<CHOICE>Product</CHOICE>
</CHOICES>
</Field>
<Field ID=”{871544BB-D045-4F61-AE3C-3084EA2E9045}”
Name=”OriginIdea”
DisplayName=”Origin of idea”
Type=”Choice”
Required=”FALSE” StaticName=”OriginIdea”>
<Default>Employee</Default>
<CHOICES>
<CHOICE>Employee</CHOICE>
<CHOICE>Consumers</CHOICE>
<CHOICE>Customers</CHOICE>
<CHOICE>Suppliers</CHOICE>
<CHOICE>Others</CHOICE>
</CHOICES>
</Field>
<Field ID=”{A83AE568-ECAE-463F-A6D9-F53F878BF874}” StaticName=”IdeaDocuments” Name=”IdeaDocuments” DisplayName=”Idea Documents” Type=”HTML” RichText=”TRUE” RichTextMode=”ThemeHtml” Required=”FALSE”/>
</List>

<List Name=”TestEvents” TemplateId=”106″ Active=”True” Url=”Lists/TestEvents”>
</List>

<List Name=”TestAnnouncement” TemplateId=”104″ Active=”True” Url=”Lists/TestAnnouncement”>
</List>

<List Name=”TestList” TemplateId=”100″ Active=”True” Url=”Lists/TestList”>

<Field ID=”{FEBDBFC8-8F74-4C68-8DFD-F34313A3B2629}”
Name=”TestCategory”
DisplayName=”Test Category”
Type=”Choice”
Required=”FALSE” StaticName=”TestCategory”>
<Default>Option1</Default>
<CHOICES>
<CHOICE>Option1</CHOICE>
<CHOICE>Option2</CHOICE>
<CHOICE>Option3</CHOICE>
</CHOICES>
</Field>
<Field ID=”{5B6F232F-115F-44E8-A191-EE42E63208DC}” RichText=”TRUE”
RichTextMode=”FullHtml” NumLines=”6″ Type=”Note” DisplayName=”Test Description” Required=”False” StaticName=”TestDescription” Name=”TestDescription” />

</List>

<Field ID=”{1A2435AC-7BE1-4511-8299-E7C5B9BF05D0}” StaticName=”Link” Name=”Link” DisplayName=”Link” Type=”URL” Format=”Hyperlink” Required=”TRUE”/>
<Field ID=”{44265EB9-56AA-45C5-BC7C-4EFDBEFEE06D}” StaticName=”Icon” Name=”Icon” DisplayName=”Icon” Type=”URL” Format=”Image” Required=”TRUE”/>
<Field ID=”{4D25B5AC-D377-4E15-BCE2-EADB8ADBB23C}” Name=”Position”
DisplayName=”Position”
Type=”Choice”
Required=”TRUE” StaticName=”Position”>
<Default>Right</Default>
<CHOICES>
<CHOICE>Right</CHOICE>
<CHOICE>Left</CHOICE>
</CHOICES>
</Field>
<Field ID=”{209E5E94-1851-4377-AA99-394351C62C14}” Indexed=”TRUE” EnforceUniqueValues=”TRUE” StaticName=”ToolOrder” Name =”ToolOrder” DisplayName =”ToolOrder” Type=”Number” Required=”TRUE” />

</List>
</Lists>
</WebSite>


Read some SharePoint tutorials:

Hope this will help.


You May Also like the Following SharePoint Online Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →

Leave a Reply