Creating a SharePoint group and adding the group to a role
using client object model
// siteURL is the string that contains the site URL
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();
string
siteUrl =
"
http://serverName:50000/sites/Testing
;
// ClientContext object is used to get the context for the SharePoint objects
ClientContext
clientContext =
new
(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();
clientContext.Load(groupColl,
groups=>groups.Include(
group=>group.Title,
group=>group.Id));
foreach
(
siteGroup
in
groupColl)
{
Console
.WriteLine(siteGroup.Title +
"-------"
+ siteGroup.Id);
}
.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.
GroupCreationInformation
groupCreationInfo =
();
groupCreationInfo.Title =
"Custom Group"
groupCreationInfo.Description=
"Custom group created using Client Object Model"
User
owner = web.EnsureUser(
@"domainName\ownerName"
);
member = web.EnsureUser(
@"domainName\userName"
group = web.SiteGroups.Add(groupCreationInfo);
group.Owner = owner;
group.Users.AddUser(member);
group.Update();
Get site all lists in list box using
SharePoint 2010 using Client Object Model.
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(); } } }