SharePoint Lists are the building blocks of SharePoint. There are many ways to create SharePoint lists. The easiest through clicks is Site Setting-> Add an app -> Choose the type -> Type Name..Done. What if you want to do this across multiple site collections. That is when the Powerful tool named PowerShell comes to your aid. This has helped me to create dummy lists needed for fulfilling certain SharePoint requirement to create custom views on a list using CSOM. I would not want to go and create a list from UI each time to do the testing. The script makes the developers life also easier.
Steps:
1. Decide the Name and Properties of the list. There are various templates in SharePoint to choose from. The most commonly used templates are
100 - Generic List
101 - Document Library
In our example, we are using a "Fruits" list of Type "GenericList" which is a Custom List.
2. Get the URL of the list where the list will be placed.
For example. http://w15-sp
3. Open the PowerShell as Administrator and write the code shown below. Please change the url and list properties as per the requirement.
#Reference to the site
$spWeb = Get-SpWEb -SIte "http://w15-sp"
#Get lists
$spLists = $spWeb.Lists
#$spLists
#List Title
$listTitle = "Fruits"
#List Description
$listDesc = "This list stores all fruits"
#List Template
#View All List Templates
#[System.Enum]::GetValues("Microsoft.SharePoint.SPListTemplateType")
$listTemplateType = [Microsoft.SharePoint.SPListTemplateType]::GenericList
$guid=$spLists.Add($listTitle,$listDesc,$listTemplateType)
"A list with GUID $guid created successfully"
Video: http://youtu.be/-iHKRQ490G4?hd=1
JavaScript CSOM to create LIst
'use strict';
var context = SP.ClientContext.get_current(); //gets the current context
var web = context.get_web(); //gets the web object
var list = web.get_lists(); //gets the collection of lists
(function () {
// This code runs when the DOM is ready and creates a context object which is
// needed to use the SharePoint object model
$(document).ready(function () {
createItem();
});
function createItem() {
// Retrieve the list and add an item to it.
var targetList = list.getByTitle("Fruits");
//create a new item object
var listItemCreation = new SP.ListItemCreationInformation();
//add the item to the list using addItem method
var newItem = targetList.addItem(listItemCreation);
var listItemTitle = document.getElementById("txtTitle").value;
alert(listItemTitle);
//var listItemCustom = document.getElementById("txtCustom").value;
//alert(listItemCustom);
//using set_item method u can set the values to the item fields
newItem.set_item('Title', listItemTitle);
//newItem.set_item('MyCustomField', listItemCustom);
newItem.update();
context.load(newItem);
context.executeQueryAsync(ItemCreationSuccess, ItemCreationFail);
}
// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function ItemCreationSuccess() {
$('#message').text('Item created successfully ');
}
// This function is executed if the above call fails
function ItemCreationFail(sender, args) {
alert('Failed to create Item' + args.get_message());
}
})();
Steps:
1. Decide the Name and Properties of the list. There are various templates in SharePoint to choose from. The most commonly used templates are
100 - Generic List
101 - Document Library
In our example, we are using a "Fruits" list of Type "GenericList" which is a Custom List.
2. Get the URL of the list where the list will be placed.
For example. http://w15-sp
3. Open the PowerShell as Administrator and write the code shown below. Please change the url and list properties as per the requirement.
#Reference to the site
$spWeb = Get-SpWEb -SIte "http://w15-sp"
#Get lists
$spLists = $spWeb.Lists
#$spLists
#List Title
$listTitle = "Fruits"
#List Description
$listDesc = "This list stores all fruits"
#List Template
#View All List Templates
#[System.Enum]::GetValues("Microsoft.SharePoint.SPListTemplateType")
$listTemplateType = [Microsoft.SharePoint.SPListTemplateType]::GenericList
$guid=$spLists.Add($listTitle,$listDesc,$listTemplateType)
"A list with GUID $guid created successfully"
Video: http://youtu.be/-iHKRQ490G4?hd=1
JavaScript CSOM to create LIst
'use strict';
var context = SP.ClientContext.get_current(); //gets the current context
var web = context.get_web(); //gets the web object
var list = web.get_lists(); //gets the collection of lists
(function () {
// This code runs when the DOM is ready and creates a context object which is
// needed to use the SharePoint object model
$(document).ready(function () {
createItem();
});
function createItem() {
// Retrieve the list and add an item to it.
var targetList = list.getByTitle("Fruits");
//create a new item object
var listItemCreation = new SP.ListItemCreationInformation();
//add the item to the list using addItem method
var newItem = targetList.addItem(listItemCreation);
var listItemTitle = document.getElementById("txtTitle").value;
alert(listItemTitle);
//var listItemCustom = document.getElementById("txtCustom").value;
//alert(listItemCustom);
//using set_item method u can set the values to the item fields
newItem.set_item('Title', listItemTitle);
//newItem.set_item('MyCustomField', listItemCustom);
newItem.update();
context.load(newItem);
context.executeQueryAsync(ItemCreationSuccess, ItemCreationFail);
}
// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function ItemCreationSuccess() {
$('#message').text('Item created successfully ');
}
// This function is executed if the above call fails
function ItemCreationFail(sender, args) {
alert('Failed to create Item' + args.get_message());
}
})();