Click here to Skip to main content
11,632,260 members (71,435 online)
Click here to Skip to main content

Tagged as

How to create custom SharePoint 2013 list using PowerShell?

, 16 Aug 2014 CPOL 8.1K 1
Rate this:
Please Sign up or sign in to vote.
How to create custom SharePoint 2013 list using PowerShell?

This post How to create custom SharePoint 2013 list using PowerShell? written by Manas Bhardwaj appeared first on Manas Bhardwaj's Stream.

SharePoint provides an interface called AddFieldAsXml. This basically creates a field based on the specified schema. Nothing fancy. Right?

However, this can be very handy when you want to create your own custom lists in SharePoint programmatically. I will be using PowerShell as an example to demonstrate how you can simply create SharePoint Lists based on plain, simple xml definitions.

To start with, I created an xml template based on which I want to create my custom list using PowerShell.

<code class="language-html">
<?xml version="1.0" encoding="utf-8"?>
<Template>
	<Field Type="Text" DisplayName="Name" Required="False" MaxLength="255" StaticName="Name" Name="Name" />
	<Field Type="Number" DisplayName="Phone Number" Required="False" MaxLength="255" StaticName="PhoneNumber" Name="PhoneNumber" />
	<Field Type="DateTime" DisplayName="Date of Birth" Required="False" MaxLength="255" StaticName="DateOfBirth" Name="DateOfBirth" /> 	
	<Field Type="Choice" DisplayName="Annual Salary" Required="False" Format="Dropdown" FillInChoice="FALSE" StaticName="AnnualSalary" Name="AnnualSalary">
            <Default>0 - 10000</Default>
            <CHOICES>
                <CHOICE>0 - 10000</CHOICE>
                <CHOICE>10000 - 50000</CHOICE>
                <CHOICE>50000 - 100000</CHOICE>
                <CHOICE>100000 or more</CHOICE>
            </CHOICES>
       </Field>	
</Template>

In this example, I have used Text, Number, Date and Choice as the Field Types. But this could be anything which is supported by SharePoint or even your own content types. Check the documentation on MSDN for Field Element.

Next step is to read this xml file, parse it and use the AddFieldAsXml method to create fields in this list. The PowerShell snippet below does the trick. Straight and Simple. Isn’t it?

<code class="language-powershell">
Add-PSSnapin Microsoft.SharePoint.PowerShell 

function CreateList($siteCollectionUrl, $listName, $templateFile){
	
	$spWeb = Get-SPWeb -Identity $siteCollectionUrl 
	$spTemplate = $spWeb.ListTemplates["Custom List"] 
	$spListCollection = $spWeb.Lists 
	$spListCollection.Add($listName, $listName, $spTemplate) 
	$path = $spWeb.url.trim() 
	$spList = $spWeb.GetList("$path/Lists/$listName")
	$templateXml = [xml](get-content $templateFile)
	foreach ($node in $templateXml.Template.Field) {
	
		$spList.Fields.AddFieldAsXml($node.OuterXml, $true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
	}
	$spList.Update()
}


$siteCollectionUrl = "http://manas.com"
$listName = "New Custom List"
$templateFile = "template.xml"

CreateList $siteCollectionUrl $listName $templateFile

And here is the result, just by configuration in xml file you can create lists using PowerShell.

SharePoint Create List

Download Example

Happy Coding!

This post How to create custom SharePoint 2013 list using PowerShell? written by Manas Bhardwaj appeared first on Manas Bhardwaj's Stream.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Manas Bhardwaj
Architect
Netherlands Netherlands
Read my personal blog at www.manasbhardwaj.net.

You may also be interested in...

Comments and Discussions

 
SuggestionDispose Pin
Nuno Nogueira27-Jan-15 23:20
memberNuno Nogueira27-Jan-15 23:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 16 Aug 2014
Article Copyright 2014 by Manas Bhardwaj
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid