Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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;
share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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;
share|improve this answer
    
Hi Andy: But this f.User is read only property.. can't assign the value of currentUser to it. –  Ankit Jun 30 '11 at 12:06
2  
Ah, okay, then it must be done with the Constructor - something like: SPFieldUserValue f= new SPFieldUserValue(web, currentUser.ID, currentUser.Name); –  Andy Burns Jun 30 '11 at 12:34
    
Yeah.. thanks.. I have done in that way... once my code is done I will past it here for other's reference.. Thanks for your help... –  Ankit Jun 30 '11 at 12:56
add comment

Usr SPFieldUserValue class.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfielduservalue.aspx

share|improve this answer
    
Hi Gandjustas: How can I use SPFieldUserValue in this case??? Can you plase give me small example for this? My problem is related to updating the list item. Whenever I am trying to update the list using itemToUpdate.Udpate(), it removes all the records from the list and adding the current user into that.. –  Ankit Jun 30 '11 at 11:00
    
sharepoint-tips.com/2007/10/… –  gandjustas Jun 30 '11 at 21:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.