Showing posts with label CSOM. Show all posts
Showing posts with label CSOM. Show all posts

Thursday, October 20, 2016

Client Object Model vs Server Object Model?

Server Object Model?
SharePoint server where the Server Object Model binaries

Example

inside Web Parts / Workflows.

Client Object Model?

client machines where the entire SharePoint binaries are not available.

Example
Windows Forms application in a client machine.

Delete item using CLient Object Model


"Client" in the namespace.  For example, in the server object model you have:
Microsoft.SharePoint.SPSite
Microsoft.SharePoint.SPWeb
Microsoft.SharePoint.SPList

In the client object model you have:
Microsoft.SharePoint.Client.Site
Microsoft.SharePoint.Client.Web
Microsoft.SharePoint.Client.List


NameSpace:


Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll


Example Code:

ClientContext context = new ClientContext("http://peakfinders.com");

Web web = context.Web;

List list = web.Lists.GetByTitle("yourlistname");
ListItemCollection listItems = list.GetItems(new CamlQuery() { ViewXml = "<View/>" });
context.Load(listItems);
context.ExecuteQuery();

listItems[listItems.Count - 1].DeleteObject();
context.ExecuteQuery();

Get By Search using Client Object Model


"Client" in the namespace.  For example, in the server object model you have:
Microsoft.SharePoint.SPSite
Microsoft.SharePoint.SPWeb
Microsoft.SharePoint.SPList

In the client object model you have:
Microsoft.SharePoint.Client.Site
Microsoft.SharePoint.Client.Web
Microsoft.SharePoint.Client.List


NameSpace:


Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll


Example Code:

ClientContext context = new ClientContext("http://peakfinders.com");
List list = context.Web.Lists.GetByTitle("yourlistname");
CamlQuery query = new CamlQuery();
query.ViewXml = @"<View>
    <Query>
        <Where>
        <Eq>
            <FieldRef Name='Status'/>
            <Value Type='Text'>Pending</Value>
        </Eq>
        </Where>
    </Query>
    </View>";

ListItemCollection listItems = list.GetItems(query);
context.Load(listItems, items => items.Include(
                                                item => item["Name"],
                                                item => item["Designation"],
                                                item => item["Status"]
                                                ));
context.ExecuteQuery();


Get By Row Limit using Client Object Model


“Client” in the namespace.  For example, in the server object model you have:

  • Microsoft.SharePoint.SPSite
  • Microsoft.SharePoint.SPWeb
  • Microsoft.SharePoint.SPList

In the client object model you have:

  • Microsoft.SharePoint.Client.Site
  • Microsoft.SharePoint.Client.Web
  • Microsoft.SharePoint.Client.List


NameSpace:


Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll


Example Code:

ClientContext context = new ClientContext("http://peakfinders.com");
List list = context.Web.Lists.GetByTitle("yourlistname");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><RowLimit>3</RowLimit></View>";

ListItemCollection listItems = list.GetItems(query);

context.Load(listItems);
context.ExecuteQuery();
context.ExecuteQuery();

Update List Items Client Object Model


“Client” in the namespace.  For example, in the server object model you have:
·         Microsoft.SharePoint.SPSite
·         Microsoft.SharePoint.SPWeb
·         Microsoft.SharePoint.SPList
In the client object model you have:
·         Microsoft.SharePoint.Client.Site
·         Microsoft.SharePoint.Client.Web
     ·         Microsoft.SharePoint.Client.List

NameSpace:


Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll


Example Code:

ClientContext context = new ClientContext("http://peakfinders.com");
List list = context.Web.Lists.GetByTitle("yourlistname");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection items = list.GetItems(query);

context.Load(items);

context.ExecuteQuery();

foreach(ListItem item in items)
    if ((item.Id % 2) == 0)
    {
        item["Title"] += "**";
        item.Update();
    }

context.ExecuteQuery();

Get List Items Client Object Model

“Client” in the namespace.  For example, in the server object model you have:
·         Microsoft.SharePoint.SPSite
·         Microsoft.SharePoint.SPWeb
·         Microsoft.SharePoint.SPList
In the client object model you have:
·         Microsoft.SharePoint.Client.Site
·         Microsoft.SharePoint.Client.Web
     ·         Microsoft.SharePoint.Client.List

NameSpace:


Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll


Example Code:

ClientContext context = new ClientContext("http://peakfinders.com");
List list = context.Web.Lists.GetByTitle("yourlistname");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection items = list.GetItems(query);

context.Load(list);
context.Load(items);

context.ExecuteQuery();

Wednesday, May 25, 2016

How Set the SiteUrl Logo using JSOM in SharePoint 2013

var context = new SP.ClientContext.get_current();
var web = context.get_web();

web.set_masterUrl('/sites/intranet2/_catalogs/masterpage/oslo.master');
web.set_siteLogoUrl('/sites/intranet2/Style%20Library/images/Contoso-Blue.png');
web.set_alternateCssUrl('/sites/intranet2/Style%20Library/CSS/Contoso.Intranet3.css');

web.update();
context.executeQueryAsync(onQuerySucceeded, onQueryFailed);

Friday, May 20, 2016

How to load all fields for a specific ListItem using Client Object Model



string str="Peakfinder";
string strlist="Employee";
Web web = context.Site.OpenWebById(str); 
List list = web.Lists.GetByTitle(strlist);
string queryTxt = "<View><Query><Where><Contains><FieldRef Name=\"Title\" /><Value Type=\"Text\">Auto</Value></Contains></Where></Query></View>"; 
CamlQuery query = new CamlQuery(); 
query.ViewXml = queryTxt;
ListItemCollection listItems = list.GetItems(query);

context.Load(listItems, items => items.Include(item => item.Id,item => item["Title"], item => item["Name"], item => item["Design"])); 
context.ExecuteQuery();

foreach (ListItem item in listItems) 
{
Console.WriteLine("Item ID = " + item.Id + " Title = " + item["Title"] + " helloworld= " + item["Name"] + " myField= " + item["Design"]);


Friday, February 12, 2016

When we get retrive People or Group field returning semicolon hash then Name

1;#Deepak Kumar


Get User name:

new SPFieldLookupValue(properties.ListItem["Staff Member"].ToString()).LookupValue;


Get User ID:

new SPFieldLookupValue(properties.ListItem["Staff Member"].ToString()).LookupId;

Using Split fuction:

properties.ListItem["Staff Member"].ToString();

You are going to be returned their userID and username, so it will always be returned separated by ;#.



properties.ListItem["Staff Member"].ToString().split(";#")[1];

Friday, February 5, 2016

How to get user displayname in SharePoint 2010 and even more in 2013 ECMA Script

ExecuteOrDelayUntilScriptLoaded(init,"sp.js");
var currentUser;

function init(){
 this.clientContext = new SP.ClientContext.get_current();
 this.oWeb = clientContext.get_web();
 currentUser = this.oWeb.get_currentUser();
 this.clientContext.load(currentUser);
 this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}
function onQuerySucceeded() {
 document.getElementById("currentUser").innerHTML = currentUser.get_loginName();
}
function onQueryFailed(sender, args) {
 alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>
<div>Current Logged-In User:<label id="currentUser"></label></div>