Tuesday, 20 May 2014

Client-Side Object Model in SharePoint 2013

The manage and javascript client object model are maintained in seprate libraries , which are located under a sharepoint system directory. The managed client object model is contained in the assemblies Microsoft.Sharepoint.ClientRuntime.dll, which we can found in the ISAPI folder.

The havascript client object model is contained in the library sp.js, which is located in the LAYOUTS folder.

Initializing the Client-side object model:  

Much like the Server -side object model, which is the other framework for development in SharePoint, CSOM also needs a starting point in the form of a central object which will instantiate and provide access to the client object model.  This central object is called the Client Context. The Client Context object orchestrates requests and initiates actions within a site collection. Once the Client Context Object is initialized, it provides information to the site collection or website through which we can access other SharePoint client object remotely as depicted in the below code.

//Managed  Client Object  Model
using (ClientContext ctx = new ClientContext(“http://MyServer/sites/MySiteCollection”))
{
Site siteCollection =ctx.site;
ctx.Load(siteCollection);
ctx.ExecuteQuery();
string url=siteCollection.Url;
}

//Javascript Client Object Model
var siteCollection;
var ctx= new SP.ClientContext.get_current();
siteCollection=ctx.get_site();
ctx.load(siteCollection);
ctx.executeQueryAsync(success,failuer);

function success()
{
var url= siteCollection.get_url();
}

function failure()
{
alert(“Failuer!”);
}

The Client Context class in managed object model inherits from the ClientContextRuntime class.
The SP.ClientContext object in JavaScript client object model inherits from the  SP.ClientContextRuntime object and provides equivalent functionality to the ClientContext  class found in the managed client object model.

Working with the Managed Client Object Model:

To start the development in the managed client object model, you will need to add references of Microsoft.SharePoint.Client.dll ,Microsoft.SharePoint.ClientRuntime.dll into the assemblies and add the using statement for Microsoft.SharePoint.Client  namespace and start writing code.

Basic operations using Managed Client Object Model:

With managed client object,  you can perform all SharePoint website related tasks, ie- Read and write all website related properties, create new SharePoint web site, SharePoint List operations(create new SharePoint lists, Retrieve all SharePoint lists in a SharePoint website, insert, update and delete in  SharePoint lists),SharePoint document library operations (same as SharePoint List operations).

Create a new SharePoint website:

Using System.Text;
Using System.Collection.Generic;
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext context = new ClientContext(“http://SiteUrl”);
WebCreationInformation creation = new WebCreationInformation();
creation.Url = “web1″;
creation.Title = “Hello web1″;
Web newWeb = context.Web.Webs.Add(creation);
// Retrieve the new web information.
context.Load(newWeb, w => w.Title);
context.ExecuteQuery();
label1.Text = newWeb.Title
New web site is created in sharepoint as a sub site.
WebCreationInformation class is used for to create a new website.

Create new SharePoint list:

// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext context = new ClientContext(“http://SiteUrl”);
// The SharePoint web at the URL.
Web web = context.Web;
ListCreationInformation creationInfo = new ListCreationInformation();
creationInfo.Title = “My List”;
creationInfo.TemplateType = (int)ListTemplateType.Announcements;
List list = web.Lists.Add(creationInfo);
list.Description = “New Description”;
list.Update();
context.ExecuteQuery();

ListCreationInformation class is used for to create new SharePoint List.
After new list is creating it update SharePoint list collection by using update method.


Working with the JavaScript Client Object Model:

The primary purpose of JavaScript client side object model is to talk with SharePoint without requiring a full-page postback. To access the JS CSOM the calling page must reference the following two files:
SP.Runtime.js
SP.js
Each of these are located in the _layouts/15/ directory under each SharePoint site; for example, http://myserver/sitename/_layouts/15/SP.js.

Whether you want to perform web site related tasks (read or write web site properties etc.), list related tasks (retrieve list properties, create and update list etc.) below are some general steps to follow for every kind of operation.

Use the ClientContext object to connect and work with the SharePoint server.
Use the clientContext.get_web() method to access specific web site object.
Use ClientContext.Load() to load the objects you need to work with.
Use the ClientContext.ExecuteQuery() to execute the query and fetch the objects

Sample method to create lists using Java Script

function createList(siteUrl) {
    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title('My Announcements List');
    listCreationInfo.set_templateType(SP.ListTemplateType.announcements);
    this.oList = oWebsite.get_lists().add(listCreationInfo);
    clientContext.load(oList);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onSuccess),
        Function.createDelegate(this, this.onFailuer)
    );
}
function onSuccess() {
    var result = oList.get_title() + ' created.';
    alert(result);
}
function onFailuer(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
}

SP.ListCreationInformation is used to create a new list in a SharePoint website.
Use get_lists().add(Parameter) to add newly created lists into web site list collection.

Refer Link

Friday, 2 May 2014

Things to remember while working with sharepoint timer job .

Important Note

Note that, whenever you change the logic or code inside Execute() method, you must restart the Timer service from Services.msc. If you just deploy without restarting service, it will not take your latest updates. So always have practice to restarting the "SharePoint Timer Service" from Services.msc.

If you still face any problem registering or deploying timer job, try uninstalling your assembly and restart the IIS and then deploy again.

To uninstall assembly, in "Run" type "assembly" and press enter. In list of assemblies, you will see the assembly of your project name. Select that and uninstall by right click.

To restart IIS, in "Run" type iisreset and press Enter.