How to automate creation of list in SharePoint 2013 in different ways ?

SharePoint lists are easy to create by any IT Pro User without any assistance of a developer. List is often the primary source of data for solutions and apps. They can be easily accessed through API’s and support full CRUD (create, read, update and delete) operations. However, there are often scenarios where it makes more sense to automate the creation of lists by using a SharePoint solution.

Declarative

New lists can be created in solutions or apps using a feature that contains a ListInstance element. The example below will create a new picture library for storing Christmas pictures

<ListInstance
 TemplateType="109"
 FeatureId = "D54C55BC-5922-4bb9-B187-1F06732B9072"
 Title="Christmas Pictures"
 Description="Trigent Christmas Picture"
 Url="List/ ChristmasPictures"
 OnQuickLaunch="TRUE" >
 </ListInstance>

 

Pros:

Declarative approach is simple as it uses tools integrates with Visual Studio 2012.

Cons:

No graceful way to handle conflicts. For example, if we try to activate a feature to create a list with the same title as an already existing one, the feature activates successfully, but the feature’s attempt to create a list silently fails due to the conflict. For apps, this is not a problem because each app instance can create its own set of lists.

Code-Based

Depending on the requirement, we can use any of the below three approaches for creating lists using code

Pros:

  • More control when creating list programmatically, we can query the list collection of the current site to check if there is an existing list with the same title before attempting to create a new list.
  • More control over configuring properties when compared to declarative. This is to support attachments and allow versioning.

Server Side Object Model:

When we can create Farm solutions, mostly for on premises implementation, the solution must be physically deployed on the server.

SPWeb site= SPContext.Current.Web;
 string ListTitle = "Christmas Pictures”;
 string ListDescription = "Trigent Christmas Pictures”;
 Guid ListId=site.Lists.Add(ListTitle, ListDescription, SPListTemplateType.PictureLibrary);
 SPList list = site.Lists[ListId];
 List.OnQuickLaunch = true;
 List.Update();

Client Side Object Model:

Provides access to SharePoint content without installing code on the server that runs Microsoft SharePoint Foundation

var ctx = new SP.ClientContext.get_current();
 var createInfo = new ListCreateInofrmation();
 createInfo.set_title(Christmas Pictures);
 createInfo.set_templatetype( SPListTemplateType.PictureLibrary);
 createInfo.set_description(Trigent Christmas Pictures);
 var newList = ctx.get_web().get_lists().add(createInfo);
 ctx.Load(newList);
 ctx.executeQueryAsync(success, failure);

REST Interface:

Allows you to integrate SharePoint capabilities into code that runs remotely in client-side or server-side applications that run on computers where SharePoint 2013 has not been installed

$.ajax({
 url: _spPageContextInfo.webServerRelativeUrl +
 "/_api/web/lists",
 Type: "POST",
 contentType: "application/json;odata=verbos",
 data: JSON.tringify(
 { '_metadata' : {'type' : 'SP.List'},
 'Title': 'Christmas Pictures',
 'BaseTemplate': 109
 })'
 Headers: {
 "accept": "application/json;odata=verbose",
 "X-RequestDigest": $("#_REQUESTDIGEST").val()
 }
 });

SharePoint 2010 Development: ADFS Users not syncing with Site collection in SharePoint 2010 server

We had two ADFS(Active Directory Federated Service) users not showing up in site collection although their user profile was being pulled properly from AD. We tried full synch, but it never worked. What worked for us were below steps in that order.

stsadm -o sync -listolddatabases 0

stsadm -o sync -deleteolddatabases 0

The listolddatabases <n>option will list the databases that have not been successfully synchronized the last <n> days, and the deleteolddatabases <n>option will delete the synchronization information from the databases that are not successfully synchronized the last <n> days. deleteolddatabases option will not delete the content databases.

stsadm –o sync –synctiming M:5

stsadm –o sync –sweeptiming M:5

The above two commands  will set the Profile Sync job and Quick Profile Sync job to run every 5 minutes.

The two users who had this problem had few stale entries in site collection, which we deleted before running the above command

SharePoint 2010 to 2013 Upgrade – Resolving Error during FIM Key Import

During the migration of user profile service application from SharePoint 2010 to SharePoint 2013 I ran into this issue, was unable to import the FIM key and were getting the below errors.

“Unable to Login to the Database”

“The Execute method of job definition Microsoft.Office.Server.UserProfiles.LMTRepopulationJob (ID f58a9431-badf-4fc5-b116-b2dd21440170) threw an exception.
More information is included below.

Object reference not set to an instance of an object.” The Farm account that we used had the required access on the source and destination DB’s of Synch, social and profile DB’s and yet we got an access denied error message. After wasting a day’s time I decided to retake the backups and restore them. It worked like a charm. The problem we faced first time was because the synch DB was backed up by our client using a windows account which had admin access on the farm and SQL. This account was not the service account and was also different from the account that we were using to create the new service apps. So be cautious of what service account you have to use while backing up the databases for SharePoint 2010 to 2013 Upgrade.

SharePoint Migration – Restricting Search by site collection using KQL in Sharepoint 2013

SharePoint 2013 Search Migration

Here’s an interesting episode that caught me engaged for a few days. Due to an overhaul in search scope in SharePoint 2013, we had to customize the whole web-part and devise methods in SharePoint 2013 environment to meet end users need.


SharePoint Migration 2010 to 2013

Earlier in SharePoint 2010, we created a custom search web part for our clients that would provision search amidst Multi-site collections. As our client migrated to SharePoint 2013, the same methods were not applicable due to changes in search scope with certain restrictions in SharePoint 2013 milieu. So we leveraged KQL (Keyword Query Language) attributes to categorise site collections and facilitated attribute based search in SharePoint 2013 environment. Here’s the code we developed while implementing the changes.

using System;
 using Microsoft.SharePoint.Client;
 using Microsoft.SharePoint.Client.Search.Query;
 namespace SearchTest
 {
 class Program
 {
 static void Main(string[] args)
 {
 using (var context = new ClientContext("http://localhost"))
 {
 var query = new KeywordQuery(context)
 {
 QueryText = "test",
 HiddenConstraints = "Department",
 };
 var executor = new SearchExecutor(context);
 var results = executor.ExecuteQuery(query);
 context.ExecuteQuery();
 if(results!=null)
 foreach (var result in results.Value[0].ResultRows)
 {
 Console.WriteLine(result["Title"]);
 }
 }
 }
 }
 }