Question

I am trying to update a list which is having User content type field, where I am saving users name selected from people picker.

Now my requirement is to add/remove a particular person in that list item. I am trying this code to add a person but it is removing all other users and adding new user in the list.. :(

I am able to add the user into the group but it's not getting added into the listitem.

Here is my code snippet::

SPWeb myweb = site.OpenWeb(); myweb.AllowUnsafeUpdates = true; SPGroup membersgroup = myweb.SiteGroups["GroupName"];

            if (SPContext.Current.Site.OpenWeb().CurrentUser != null && membersgroup != null)
            {
                membersgroup.AddUser(SPContext.Current.Site.OpenWeb().CurrentUser);

                SPRoleDefinition role = myweb.RoleDefinitions["Contribute"];
                SPRoleAssignment roleAssignment = new SPRoleAssignment(membersgroup);
                roleAssignment.RoleDefinitionBindings.Add(role);
                myweb.BreakRoleInheritance(true);
                myweb.RoleAssignments.Add(roleAssignment);
                myweb.Update();

                SPList settingsLists = myweb.Lists["CommunitySettings"];
                SPListItem itemToUpdate = settingsLists.Items[0];
                itemToUpdate["MembersJoined"] = SPContext.Current.Site.OpenWeb().CurrentUser;
                itemToUpdate.Update();
                settingsLists.Update();
            }
            myweb.AllowUnsafeUpdates = false;

Please help me in adding and also removing the user.

thanks in advance.


Here is the answer...

myweb.AllowUnsafeUpdates = true;

        SPGroup membersgroup = myweb.SiteGroups[string.Format("{0}-Member", myweb.Title)];

        if (SPContext.Current.Site.OpenWeb().CurrentUser != null && membersgroup != null)
        {
            membersgroup.AddUser(SPContext.Current.Site.OpenWeb().CurrentUser);

            SPRoleDefinition role = myweb.RoleDefinitions["Contribute"];
            SPRoleAssignment roleAssignment = new SPRoleAssignment(membersgroup);
            roleAssignment.RoleDefinitionBindings.Add(role);
            myweb.BreakRoleInheritance(true);
            myweb.RoleAssignments.Add(roleAssignment);
            myweb.Update();

            SPList settingsLists = myweb.Lists["CommunitySettings"];

            SPListItem itemToUpdate = settingsLists.Items[0];
            SPUser currentUser = SPContext.Current.Site.OpenWeb().CurrentUser;
            SPFieldUserValue userValue = new SPFieldUserValue(myweb, currentUser.ID, currentUser.Name);

            Microsoft.SharePoint.SPFieldUserValueCollection collection = (Microsoft.SharePoint.SPFieldUserValueCollection)itemToUpdate["MembersJoined"];
            collection.Add(userValue);
            itemToUpdate["MembersJoined"] = collection;
            itemToUpdate.Update();
            settingsLists.Update();
        }
        myweb.AllowUnsafeUpdates = false;
Was it helpful?

Solution

You shouldn't be making all those calls to OpenWeb() - check your disposal patterns. Lots of information on that if you Search Google.

Thant aside, to set the value on the item, I think you want something like

SPUser currentUser = ...
SPFieldUserValue f = new SPFieldUserValue();
f.User = currentUser;
item["MembersJoined"] = f;
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top