sreenu kolusu

sreenu kolusu

  • NA
  • 38
  • 181k

managed Client object model in sharepoint2010

Aug 9 2012 2:43 PM

// siteURL is the string that contains the site URL
string siteUrl = "http://demoshare:9999";
// ClientContext object is used to get the context for the SharePoint objects
ClientContext clientContext = new ClientContext(siteUrl);
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("Administrator", "@Nivista123", "sp.com");
clientContext.Credentials = credentials;
Web web = clientContext.Web;
GroupCreationInformation groupCreationInfo = new GroupCreationInformation();
groupCreationInfo.Title = "Custom Group3";
groupCreationInfo.Description = "Custom group created using Client Object Model3";
Group oGroup = web.SiteGroups.Add(groupCreationInfo);

RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);

RoleDefinition oRoleDefinition = web.RoleDefinitions.GetByType(RoleType.Contributor);

collRoleDefinitionBinding.Add(oRoleDefinition);

web.RoleAssignments.Add(oGroup, collRoleDefinitionBinding);
User owner = web.EnsureUser(@"SP\uday");
User member = web.EnsureUser(@"SP\obulreddy");

Group group = web.SiteGroups.Add(groupCreationInfo);
group.Owner = owner;
group.Users.AddUser(member);
group.Update();

clientContext.ExecuteQuery();
MessageBox.Show("group successfully created")
Assigning a user to a role on a Web site using client object model

            ClientContext oClientContext = new ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite");
            Web oWebsite = clientContext.Web;

            Principal oUser = oWebsite.SiteUsers.GetByLoginName(@"DOMAIN\alias");

            RoleDefinition oRoleDefinition = oWebsite.RoleDefinitions.GetByName("Create and Manage Alerts");
            RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);
            collRoleDefinitionBinding.Add(oRoleDefinition);

            RoleAssignment oRoleAssignment = oWebsite.RoleAssignments.Add(oUser, collRoleDefinitionBinding);

            clientContext.Load(oUser,
                user => user.Title);

            clientContext.Load(oRoleDefinition,
                role => role.Name);

            clientContext.ExecuteQuery();

Delete the group from SharePoint 2010 site using Client Object Model

// siteURL is the string that contains the site URL 
            string siteUrl = "http://serverName:50000/sites/Testing";
            // ClientContext object is used to get the context for the SharePoint objects
            ClientContext clientContext = new ClientContext(siteUrl);
            Web web = clientContext.Web;
            Group testingVisitorsGroup = web.SiteGroups.GetById(7);
            GroupCollection groupColl = web.SiteGroups;
            groupColl.Remove(testingVisitorsGroup);        
            clientContext.ExecuteQuery();   

Get all the site groups in SharePoint 2010 using Client Object Model

// siteURL is the string that contains the site URL 
            string siteUrl = "http://serverName:50000/sites/Testing";   
            // ClientContext object is used to get the context for the SharePoint objects
            ClientContext clientContext = new ClientContext(siteUrl);
            Web web = clientContext.Web;
            GroupCollection groupColl = web.SiteGroups;
            clientContext.Load(groupColl,
                groups=>groups.Include(
                group=>group.Title,
                group=>group.Id));
            clientContext.ExecuteQuery();
            foreach (Group siteGroup in groupColl)
            {
                Console.WriteLine(siteGroup.Title + "-------" + siteGroup.Id);                
            }
            Console.ReadLine();

Create a site group in SharePoint 2010 using Client Object Model

 // siteURL is the string that contains the site URL 
            string siteUrl = "http://serverName:50000/sites/Testing";
            // ClientContext object is used to get the context for the SharePoint objects
            ClientContext clientContext = new ClientContext(siteUrl);
            Web web = clientContext.Web;   
        
            GroupCreationInformation groupCreationInfo = new GroupCreationInformation();
            groupCreationInfo.Title = "Custom Group";
            groupCreationInfo.Description="Custom group created using Client Object Model";
 
            User owner = web.EnsureUser(@"domainName\ownerName");
            User member = web.EnsureUser(@"domainName\userName");
 
            Group group = web.SiteGroups.Add(groupCreationInfo);
            group.Owner = owner;
            group.Users.AddUser(member);
            group.Update();       
            
            clientContext.ExecuteQuery();           
        }
Get site all lists in list box using SharePoint 2010 using Client Object Model.
listBox1.Items.Clear();
// siteURL is the string that contains the site URL
string siteUrl = "http://demoshare:6969";
// ClientContext object is used to get the context for the SharePoint objects
ClientContext clientContext = new ClientContext(siteUrl);
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("Administrator", "@Nivista123", "sp.com");
clientContext.Credentials = credentials;
var web = clientContext.Web;

clientContext.Load(web);
clientContext.Load(web.Lists);

clientContext.ExecuteQuery();

foreach (List list in web.Lists)
{
listBox1.Items.Add(list.Title);
}

How to: Create, Update, and Delete List Items

Creating a list item
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateListItem
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(itemCreateInfo);
            oListItem["Title"] = "My New Item!";
            oListItem["Body"] = "Hello World!";

            oListItem.Update();

            clientContext.ExecuteQuery(); 
        }
    }
}



using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class UpdateListItem
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
            ListItem oListItem = oList.Items.GetById(3);

            oListItem["Title"] = "My Updated Title.";

            oListItem.Update();

            clientContext.ExecuteQuery(); 
        }
    }
}
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class DeleteListItem
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
            ListItem oListItem = oList.GetItemById(2);

            oListItem.DeleteObject();

            clientContext.ExecuteQuery(); 
        }
    }
}