3

I am working on developing an event receiver inside my sharepoint 2013 farm on-premises. and inside the event receiver i am trying to assign a list column to a SPGroup.

what i am trying to do is similar to the following steps using the UI:-

  1. Go to a list settings.
  2. click on the column at the list level.
  3. override the column to be linked to a specific group (for example Approvers) instead of All Users as defined inside the site column level. enter image description here

Now here is my code inside the ER:-

SPGroup newgroup = site.RootWeb.SiteGroups[currentgroupname + " Members - " + curItemID.ToString()];
foreach (SPList splist in newSite.Lists)
 {
   if (splist.Title.ToLower() == "action")
       {
         SPFieldUser spfield = splist.Fields["ActionOwner"] as SPFieldUser;
         spfield =  newgroup as SPGroup;
       }
  }

But i am getting the following syntax error on this spfield = newgroup as SPGroup;

Cannot implicitly convert type 'Microsoft.SharePoint.SPGroup' to 'Microsoft.SharePoint.SPFieldUser'

1 Answer 1

4

Try:

spfield.SelectionGroup = newgroup.ID;
spfield.Update();
spList.Update();

MSDN: SPFieldUser.SelectionGroup property

Sample: Programmatically Set "Person Or Group" Column To Select User From A Specified Group

6
  • thanks for the code,,, will try it ,, but i have a question ,, if i am modifying the list column at the list level using the code you provide,, then still i can override all the list columns if i update the column at the site level and chose to apply the changes to all the sub-sites ?? similar to doing things using the UI ???
    – John John
    Jul 4, 2017 at 14:34
  • 1
    I think is it possible. It is a site column? Jul 4, 2017 at 14:40
  • it is a site column, and inside the code i want to override it inside the lists level... but i still like the option to have the control on overriding all the list columns by updating their parent site column.. now if i use the UI then i will have this option
    – John John
    Jul 4, 2017 at 14:40
  • 1
    You can use Update(true). From MSDN: Updates the database with changes that are made to the field and optionally propagates the changes to lists that implement the field. msdn.microsoft.com/en-us/library/ms454698.aspx Jul 4, 2017 at 14:42
  • no i am asking different thing.. now i test at my end where using the code i assign the ActionOwner to the new group.. then i went to the site column and the chose to update the ActionOwner and propagate the change to all the list columns,, and the list column where changed from being assigned to the new group to be assigned to "All Users" as defined inside the site level.... so the code will update the list column as i am usng the UI ,, which is great..
    – John John
    Jul 4, 2017 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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