public void UpdateGroup(ObservableCollection<PermissionLevel> permissionLevels,bool canRemove, string siteUrl, bCheck.Admin.Data.Group gp, Action<bool, Exception> reply)
        {
            ClientRequestSucceededEventHandler SuccessHandler = null;
            ClientRequestFailedEventHandler FailureHandler = null;

            Principal principal = null;

            ClientContext ictx = null;
            if (siteUrl == Constants.Optional)
                ictx = _client;
            else
                ictx = new ClientContext(siteUrl);

            if (gp.Owner == null)
            {
                if (gp.EditOwner.PrincipalType == PrincipalType.SharePointGroup)
                    principal = ictx.Web.SiteGroups.GetById(gp.EditOwner.Id);
                else if (gp.EditOwner.PrincipalType == PrincipalType.User)
                    principal = ictx.Web.EnsureUser(gp.EditOwner.LoginName);
            }
            else
            {
                if (gp.Owner.PrincipalType == PrincipalType.SharePointGroup)
                    principal = ictx.Web.SiteGroups.GetById(gp.Owner.PrincipalId);
                else if (gp.Owner.PrincipalType == PrincipalType.User)
                    principal = ictx.Web.EnsureUser(gp.Owner.LoginName);
            }

            Group group = ictx.Web.SiteGroups.GetById(gp.Id);
            group.Title = gp.Title;
            group.Owner = principal;
            group.AllowMembersEditMembership = gp.AllowMembersEditMembership;
            group.OnlyAllowMembersViewMembership = gp.OnlyAllowMembersViewMembership;            
            group.Update();

            if (canRemove)
            {
                RoleAssignment gpRss = ictx.Web.RoleAssignments.GetByPrincipalId(gp.Id);
                gpRss.RoleDefinitionBindings.RemoveAll();
                gpRss.Update();
            }

            if (permissionLevels != null && permissionLevels.Count > 0)
            {
                RoleDefinitionBindingCollection rbc = new RoleDefinitionBindingCollection(ictx);

                foreach (PermissionLevel pl in permissionLevels)
                {
                    RoleDefinition rd = ictx.Web.RoleDefinitions.GetByName(pl.LevelName);
                    rbc.Add(rd);
                }
                ictx.Web.RoleAssignments.Add(group, rbc);
            }
            
            
            SuccessHandler = (s, e) =>
            {
                reply(true, null);
            };

            FailureHandler = (s, e) =>
            {
                Logger.AddLog(_log, e.Exception);
                reply(false, e.Exception);
            };

            ictx.ExecuteQueryAsync(SuccessHandler, FailureHandler);
        }
Exemple #2
0
        void wssGroups_GetGroupCollectionFromUserCompleted(object sender, bCheck.Admin.UserGroupsService.GetGroupCollectionFromUserCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                XElement el = e.Result;
                XNamespace ns = "http://schemas.microsoft.com/sharepoint/soap/directory/";
                var queryGroups = from x in el.Descendants()
                                  where x.Name == ns + "Group"
                                  select new { GroupName = (string)x.Attribute("Name") };
                mainPage.AddToLog("Found group membership");
                foreach (var grp in queryGroups)
                {
                    this.MemberOfGroups.Add(grp.GroupName);
                    mainPage.AddToLog(grp.GroupName);
                }

                if (UserGroupsRetrieved != null)
                    UserGroupsRetrieved(sender, e);
            }
            else
            {
                mainPage.AddToLog("Error obtaining group membership information");
                mainPage.AddToLog(e.Error.ToString());
                throw new Exception(
                    string.Format("Unable to fetch security information for login user:{0}\r\n{1}", this.LoginID, e.Error.ToString())
                    );
            }
        }