Pages

Showing posts with label Client Object Model. Show all posts
Showing posts with label Client Object Model. Show all posts

Monday, June 4, 2012

How to Initialized Client Context in Office 365 / Claim Based Authenticated Sites

Problem:

Generally we initialize the client context straight away by creating new object of class ClientContext and by providing the url of the site as well as needed credentials, but for sites using claims based authentication (let it be Office 365 site or On Premise site) this logic won’t, so what’s the solution?

Solution:
User must be logged in to the Federated Authentication Provider before initializing the client context object and hence there should be some provision in the client application to let users log in.
Microsoft has released a sample console application which demonstrates about remote authentication in SharePoint Online / Claim based SharePoint sites using managed client object model.

Hope this helps someone.

Tuesday, January 3, 2012

How to set people and group column programmatically using Client Object Model


using SP = Microsoft.SharePoint.Client;
using (SP.ClientContext _ctx = new SP.ClientContext("http://YourSiteUrl"))
{
  SP.ListItem _listItem = null;

  ctx.Credentials = CredentialCache.DefaultCredentials;
 
  SP.List _testList = web.Lists.GetByTitle("TestList");
  SP.CamlQuery _query = new SP.CamlQuery();
  query.ViewXml = @"<View><Query><Where><Geq><FieldRef Name='ID'/>" + "<Value Type='Number'>10</Value></Geq></Where></Query></View>";
  SP.ListItemCollection _listitems = _configList.GetItems(_query);

  ctx.Load(_listitems);
  ctx.ExecuteQuery();
 
  if (_listitems != null && _listitems.Count > 0)
  {
     _listItem = _listitems[0];

     SP.FieldUserValue _userValue = new SP.FieldUserValue();
     SP.User _newUser = _ctx.Web.EnsureUser("domain\\username");
     _ctx.Load(_newUser);
     _ctx.ExecuteQuery();

     _userValue.LookupId = _newUser.Id;

     _listItem["userfield"] = _userValue;
     _listItem.Update();

     _ctx.ExecuteQuery();

  }   

);