Thursday, 17 March 2016

SharePoint Lists Creation using CSOM

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());
    }
})();

Wednesday, 16 March 2016

SharePoint Views using CSOM

This post is regarding a recent SharePoint requirement related to creating views using CSOM.
Once upon a time, I used to play with the Content Editor Web in MOSS 2007 and could happily satisfy the customer requirements without the need to do server side coding , and also not lose out on the joy of coding. A web part page was created and Content Editor Web Part was dropped on the page which contained the code mostly using jQuery. Things have improved soo much in SharePoint 2013 version. I got a requirement to create views from SharePoint page using CSOM. I started off trying to create the view from SharePoint User Interface.Then, with the help of PowerShell, I got the Query needed for the CSOM. I almost got stuck with the part where the view needed to be updated with Scope=Recursive. Initially, I tried through SPViewInformation and this did not support much properties.I tried though CAML query <QueryOptions><ViewAttributes Scope='Recursive'></QueryOptions> which was not working in SharePoint 2013.Later, thankfully, like magic, I was taken to a msdn site which indicated the SP.ViewScope property.The page with the code will be updated soon for a more generic example to Create Views for a simple Document Library and a Custom List  in SharePoint.


References:
https://msdn.microsoft.com/en-us/library/office/ee548053(v=office.14).aspx