1

I have a console app, it updates people or group field of sharepoint list. If the item["Approver"] value is a user value this app works fine. If the item["Approver"] value is a sharepoint group this will fail. I expect this app should work if the item["Approver"] value is both user or group. Can someone suggest the code part to make me success?

static  SPWeb _web;
        static SPSite _site;
        static SPList myList;
        static void Main(string[] args)
        {
            string usercontrolvalue = "test";
            _site = new SPSite("URL");
            _web = _site.OpenWeb();
            _web.AllowUnsafeUpdates = true;
            myList = _web.Lists["MYList"];
            SPListItem item = myList.Items.Add();
            SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
            string[] userarray = usercontrolvalue.Split(',');
            for (int i = 0; i < userarray.Length; i++)
            {
                SPFieldUserValue usertoadd = ConvertLoginName(userarray[i]);
                usercollection.Add(usertoadd);
            }
            item["Approver"] = usercollection;
            item.Update();
        }


        public static SPFieldUserValue ConvertLoginName(string userid)
        {
            SPUser requireduser = _web.EnsureUser(userid);
            SPFieldUserValue uservalue = new SPFieldUserValue(_web, requireduser.ID, requireduser.LoginName);
            return uservalue; 
        }
  • 2
    Are you getting any error while updating? – Aanchal Sep 17 '14 at 9:28
  • Invalid user "test", while test is a SharePoint group. – Sivakumar Piratheeban Sep 17 '14 at 9:40
3

Change the ConvertLoginName method to:

public static SPFieldUserValue ConvertLoginName(string userid)
{
    // Try to get the SPUser value if userid is a user or AD group that has been imported into SharePoint by the UPS.
    try {
        SPUser requireduser = _web.EnsureUser(userid);
        SPFieldUserValue uservalue = new SPFieldUserValue(_web, requireduser.ID, requireduser.LoginName);
        return uservalue;
    } catch {}

    // Try to get the SPGroup value if userid is a SharePoint group in the current site collection.
    try {
        SPGroup requiredgroup = _web.SiteGroups[userid];
        SPFieldUserValue uservalue = new SPFieldUserValue(_web, requiredgroup.ID, requiredgroup.LoginName);
        return uservalue;
    } catch { }

    // Return null if the userid cannot be resolved to a SharePoint user or group.
    return null;
}


Also, you may want to change the body of the for loop to check if the ConvertLoginName method returns null.


Remarks:

  • I have not tested the code, but it is similar to the code I wrote in a PowerShell script, so it should work.
  • The new ConvertLoginName method assumes that the userid string has the following form for SharePoint users: domain\username or email address; the string for SharePoint group needs to be the name of the group.
1

You can use following extension methods to update the user/group field:

Single User/Group

public static void SetUserValue(this SPListItem item, string columnName, int userId)
{
    SPWeb web = item.Web;
    item[columnName] = new SPFieldUserValue(web, userId.ToString());
}

Multiple User/Group

public static void SetUserValueCollection(this SPListItem item, string columnName, int[] userIds)
{
    SPWeb web = item.Web;
    var value = new SPFieldUserValueCollection();
    value.AddRange(userIds.Select(i => new SPFieldUserValue(web, i.ToString())));
    item[columnName] = value;
}

public static void SetUserToValueCollection(this SPListItem item, string columnName, int userId)
{
    SPWeb web = item.Web;
    SPFieldUserValueCollection value = (item[columnName] != null) 
        ? value = item[columnName] as SPFieldUserValueCollection 
        : value = new SPFieldUserValueCollection();

    value.Add(new SPFieldUserValue(web, userId.ToString()));
    item[columnName] = value;
}

You can find extension methods for SPListItem at following link: http://customsp.blogspot.com/2014/10/SPListItem-Extension-Methods-to-get-and-set-field-values.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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