Friday 21 November 2014

SPServices UpdateListItems batchUpdate

Below code is updating AssignedTo field in Task List for multiple items in List

//for updating multiple items in tasks list
var update_string="<Batch OnError='Continue'>";
var count=0;

for(var i=0;i<ArrayTest.length;i++)  // ArrayTest is having list items IDs
{
count++;
update_string = update_string+"<Method ID='"+count+"' Cmd='Update'>" +
"<Field Name='ID'>"+ArrayTest[i]+"</Field>" +
"<Field Name='AssignedTo'>TestUser</Field>" +
"</Method>";  
}
update_string = update_string+"</Batch>";

$().SPServices({
operation: "UpdateListItems",
async: true,
batchCmd: "Update",
listName: "Tasks",
updates: update_string,
completefunc: function(xData, Status)
{
//alert(xData.responseText);
}
});

SPServices GetListItems Operation

//Add reference in js file
<script src="/js/jquery.SPServices-0.7.2.min.js"type="text/javascript"></script>

//CAML Query that will be used in SPServices GetListItems Operation
var funderQuery = "<Query>" +
"<Where>" +
                                "<Eq>"+
"<FieldRef Name='ID' /><Value Type='Text'>"+thisItemID+"</Value>"+
"</Eq>"+
"</Where>" +
"</Query>";

$().SPServices(
{
operation: "GetListItems",  
async: false,        
listName: "ListName",
CAMLViewFields: "<ViewFields><FieldRef Name='FieldInternalName' />     </ViewFields>",
CAMLQuery: funderQuery,
completefunc: function (xData, Status)
{      
if (Status == "success")
{      
//alert(xData.responseText);
$(xData.responseXML).SPFilterNode("z:row").each(function()
{
var field = $(this).attr("ows_FieldInternalName");
});
}
}
});

Wednesday 22 October 2014

REST Services CRUD Operations in Sharepoint 2013

REST Services – High Level Overview

Let’s start out with our basic get commands in REST. Below is a list of the basic commands used to get List Items from a SharePoint List through the SharePoint 2013 REST Services.

COMMAND

URL
Get All Lists
http://server/site/_api/lists
Get All List Items From a Single List
http://server/site/_api/lists/getbytitle(‘listname’)/items
Get a Single List Item
http://server/site/_api/lists/getbytitle(‘listname’)/items
Get Back Certain Columns
http://server/site/_api/lists/getbytitle(‘listname’)/items?$select=Title,Id
Order Your Results
 http://server/site/_api/lists/getbytitle(‘listname’)/items?$orderby=Title

CRUD operations using REST in Sharepoint 2013

Get a Single List Item

function getListItem(url, listname, id, complete, failure) 
{
        // Getting our list items
        $.ajax({
                url: url + "/_api/web/lists/getbytitle('" + listname + "')/items(" + id + ")",
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },
                success: function (data) {
                        // Returning the results
                        complete(data);
                },
                error: function (data) {
                        failure(data);
                }
                });
        }
}


Creating list items gets a little bit tricky, because you’ll need a few key pieces of information:
- The List Item type 
- REQUESTDIGEST value to prevent replay attacks
- An object containing your List Item Values
// Adding a list item with the metadata provided
function addListItem(url, listname, metadata, success, failure) 
{
    // Prepping our update
    var item = $.extend({
        "__metadata": { "type": getListItemType(listname)}
    }, metadata);

    // Executing our add
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            success(data); // Returns the newly created list item information
        },
        error: function (data) {
            failure(data);
        }
    });
}


Updating list items is very similar to the task of creating list items when using jQuery Ajax calls.  You’ll need the same information you used then you created your list item, with one more piece of information: 
- The List Item Type 
- REQUESTDIGEST 
- An object containing your List Item Values & Item Type 
- The REST URL to your List Item (We use our getListItem function to get this reliably, See Above)
- Stored in: data.d.__metadata.uri
function updateListItem(url, listname, id, metadata, success, failure)
{
    // Prepping our update
    var item = $.extend({
        "__metadata": { "type": getListItemType(listname) }
    }, metadata);

    getListItem(url, listname, id, function (data) {
        $.ajax({
            url: data.d.__metadata.uri,
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(item),
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "MERGE",
                "If-Match": data.d.__metadata.etag
            },
            success: function (data) {
                success(data);
            },
            error: function (data) {
                failure(data);
            }
        });

    }, function (data) {
        failure(data);
    });
}
Deleting a list item also requires the REST URL that leads to the List Item you would like to delete.  Again, we get this through our getListItem function (See Above).  So you’ll need: 
- The List Item Type 
- REQUESTDIGEST 
- REST URL to your List Item (via our getListItem function)
// Deleting a List Item based on the ID
function deleteListItem(url, listname, id, success, failure) 
{
    // getting our item to delete, then executing a delete once it's been returned
    getListItem(url, listname, id, function (data) {
        $.ajax({
            url: data.d.__metadata.uri,
            type: "POST",
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-Http-Method": "DELETE",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "If-Match": data.d.__metadata.etag
            },
            success: function (data) {
                success(data);
            },
            error: function (data) {
                failure(data);
            }
        });
    });
};

Thursday 16 October 2014

CRUD operation using SPServices

Get List Items Using SPServices


$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Listname",
    CAMLQuery: "<Query><Where><IsNotNull><FieldRef Name='Phase' /></IsNotNull></Where>                           <GroupBy Collapse='True'><FieldRef Name='Phase'></FieldRef></GroupBy>                                   </Query>";
    CAMLViewFields: ""<ViewFields><FieldRef Name='ID' /><FieldRef Name='Phase' /><FieldRef                                 Name='DueDate' />",
    completefunc: function (xData, Status)
{      
if (Status == "success")
{                                    
$(xData.responseXML).SPFilterNode("z:row").each(function()
{
var Phase = $(this).attr("ows_Phase");
var DueDate = $(this).attr("ows_DueDate");
});
}
}
});



Delete All Items from List

function DeleteAllItemsFromList()
  {
    $.SPServices.SPUpdateMultipleListItems({
    listName: "Listname",
    CAMLQuery: "<Query><Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where></Query>",
    batchCmd: "Delete"});
  }



Add Item to List

$.SPServices({
   operation: 'UpdateListItems',
   listName: "Listname",
   updates: '<Batch OnError="Continue">' +
             '<Method ID="1" Cmd="Update">' +
  '<Field Name="ID">'+ ItemID +'</Field>' +
             '<Field Name="Title">'+ title +'</Field>' +
             '</Method>' +
             '</Batch>',
   completefunc: function(xData, Status)
        {}
});

Monday 3 March 2014

Client Object Model and Server Object Model in SharePoint 2010

Server Object Model:
1-SharePoint itself uses the server object model.You can also use this model on a sharepoint server bcz it has some dependencies satisfied by the sharepoint server.
2-Here you can use LINQ programming like LINQ to SharePoint.
3-different classes are there like SPFarm, SPServer, SPService, and SPWebApplication etc.
 
Client Object Model:
1-Client Side APIs refer to the Client Object Models that were introduced in SharePoint 2010.
2-It is available in three different flavors like: .NET managed,Silverlight and ECMAScript.
3-The Main advantage of the CSOMs is that you do not need to be on the Server to use them in your program. The .NET client object can be used in any Desktop or Web Client. The Silverlight CSOM can be used in silverlight applications and you can call the JS Client Object Model directly from your web pages.
 
Managed Object Model
Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.ClientRuntime.dll
ISAPI folder
Silverlight client object model
Microsoft.SharePoint.Client.Silverlight.dll
Microsoft.SharePoint.Client.Silverlight.Runtime.dll
LAYOUTS\ClientBin folder
JavaScript client object model
SP.js
LAYOUTS folder
Different types of Objects:
Server object model
Managed Model
Silverlight Model
Javascript Model
SPContext
ClientContext
ClientContext
ClientContext
SPSite
Site
Site
Site
SPWeb
Web
Web
Web
SPList
List
List
List
SPListItem
ListItem
ListItem
ListItem
SPField
Field
Field
Field

Types of Event Receivers in SharePoint

List Events
  • A field was added
  • A field is being added
  • A field was removed
  • A field is being removed
  • A field was updated
  • A field is being updated
  • A list is being added
  • A list is being deleted
  • A list was added
  • A list was deleted
List Item Events
  • An item is being added
  • An item is being updated
  • An item is being deleted
  • An item is being checked in
  • An item is being checked out
  • An item is being unchecked out
  • An attachment is being added to the item
  • An attachment is being removed to the item
  • A file is being moved
  • An item was added
  • An item was updated
  • An item was deleted
  • An item was checked in
  • An item was checked out
  • An item was unchecked out
  • An attachment was added to the item
  • An attachment was removed to the item
  • A file was moved
  • A file was converted
  • The list received a context event
List Email Events
  • The list received an e-mail message
Web Events
  • A site collection is being deleted
  • A site is being deleted
  • A site is being moved
  • A site is being provisioned
  • A site collection was deleted
  • A site was deleted
  • A site was moved
  • A site was provisioned
List Workflow Events
  • A workflow  is starting
  • A workflow was started
  • A workflow was postponed
  • A workflow was completed