Create a list in SharePoint online with the help schema file using PowerShell/CSOM
We required two XML configuration files
- first one is get major info like Site Url, Username
<?xml version="1.0" encoding="utf-8"?>
<WebSite Url="https://contoso.sharepoint.com/sites/SharepointOnine/" >
<User Name="nagaraju.p@contoso.com" ></User>
</WebSite>
- We have place list schema files in one folder then below script will take care to create multiple list.
<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ows="Microsoft SharePoint" Title="Client Profile" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/ClientProfile" BaseType="0"
xmlns="http://schemas.microsoft.com/sharepoint/">
<MetaData>
<ContentTypes>
<ContentType ID="0x0100DC5C3AA235E71E498232D6A6F20FDEAD" Name="$Resources:core,Item;" Group="$Resources:core,List_Content_Types;" Description="$Resources:core,ItemCTDesc;">
<Folder TargetName="Item"/>
<FieldRefs>
<FieldRef Name="Title" DisplayName="$Resources:core,Title;" Required="TRUE"/>
<FieldRef Name="PartnerName" DisplayName="Partner Name" Required="TRUE"/>
<FieldRef Name="NADescription" DisplayName="Description" Required="TRUE" Format="Image"/>
<FieldRef Name="NotifyUsers" DisplayName="Notify Users" Required="TRUE"/>
<FieldRef Name="Active" DisplayName="Active" Required="FALSE"/>
<FieldRef Name="Leader" DisplayName="Leader" Required="FALSE"/>
</FieldRefs>
</ContentType>
<ContentTypeRef ID="0x01">
<Folder TargetName="Item" />
</ContentTypeRef>
<ContentTypeRef ID="0x0120" />
</ContentTypes>
<Fields>
<Field Type="Text" Name="Title" DisplayName="Title" Required="TRUE" StaticName="Title" MaxLength="150" />
<Field Type="User" Name="PartnerName" DisplayName="User Name" Required="TRUE" StaticName="PartnerName" UserSelectionMode="PeopleOnly" UserSelectionScope="0"></Field>
<Field Type="Note" Name="NADescription" DisplayName="Description" Required="TRUE" NumLines="6"></Field>
<Field Type="Choice" Name="NotifyUsers" DisplayName="NotifyUsers" Required="FALSE" Format="Dropdown">
<CHOICES>
<CHOICE>Yes</CHOICE>
<CHOICE>No</CHOICE>
</CHOICES>
<Default>No</Default>
</Field>
<Field Type="DateTime" Name="StartDate" DisplayName="Start Date" Format="DateTime">
</Field>
<Field Type="URL" DisplayName="Partner Conatct Image" Format="Image" Name="PartnerConatctImage">
</Field>
<Field Type="Boolean" DisplayName="Active" Name="Active">
<Default>0</Default>
</Field>
</Fields>
<Views>
<View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE">
<Toolbar Type="Standard" />
<XslLink Default="TRUE">main.xsl</XslLink>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="LinkTitleNoMenu"></FieldRef>
<FieldRef Name="PartnerName"></FieldRef>
</ViewFields>
<Query>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
</OrderBy>
</Query>
<ParameterBindings>
<ParameterBinding Name="AddNewAnnouncement" Location="Resource(wss,addnewitem)" />
<ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
<ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_ONET_HOME)" />
</ParameterBindings>
</View>
<View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/15/images/generic.png?rev=23" Url="AllItems.aspx">
<Toolbar Type="Standard" />
<XslLink Default="TRUE">main.xsl</XslLink>
<JSLink>clienttemplates.js</JSLink>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="LinkTitle"></FieldRef>
<FieldRef Name="NADescription"></FieldRef>
</ViewFields>
<Query>
<OrderBy>
<FieldRef Name="ID"></FieldRef>
</OrderBy>
</Query>
<ParameterBindings>
<ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
<ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_DEFAULT)" />
</ParameterBindings>
</View>
</Views>
<Forms>
<Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
<Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
<Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
</Forms>
</MetaData>
</List>
- Finally run the below PowerShell/CSOM script.
$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$xmlFilePath = $("$dp0\WW-DeployConfig.xml")
$xmldata = [xml](Get-Content($xmlFilePath));
#Specify tenant admin and site URL
$User = $xmldata.WebSite.User.Name
$webUrl = $xmldata.WebSite.Url
$schemaXmlFilePath = $("$dp0\ListSchema\SampleList.xml")
Write-Host "Input List schema file URL: " $schemaXmlFilePath
$listTemplateId = "100"
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
if (!$ctx.ServerObjectIsNull.Value)
{
Write-Host "Connected to SharePoint Online site: '$SiteURL'" -ForegroundColor Green
}
#Bind to site collection
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$ctx.Credentials = $Creds
#Retrieve lists
$Lists = $ctx.Web.Lists
$ctx.Load($Lists)
$ctx.Load($ctx.Web.ListTemplates)
Write-Host "Loading list templates.."
$ctx.ExecuteQuery()
$files = Get-ChildItem $("$dp0\ListSchema")
for ($i=0; $i -lt $files.Count; $i++) {
$outfile = $files[$i].FullName
Write-Host "Input XML file url: "$outfile
$schemaXml = [xml] (Get-Content $outfile)
$template = $ctx.Web.ListTemplates | WHERE { $_.ListTemplateTypeKind -eq $listTemplateId }
$info = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$info.Title = $schemaXml.List.Title
Write-Host "List Title: "$schemaXml.List.Title
$info.Url = $schemaXml.List.Url
$info.CustomSchemaXml = $schemaXml.OuterXml
$info.TemplateType = $listTemplateId
$info.TemplateFeatureId = $template.FeatureId
$list = $ctx.Web.Lists.Add($info)
$ctx.Load($list)
Write-Host $schemaXml.List.Title" list is Creating.."
$ctx.ExecuteQuery()
}
No comments:
Post a Comment