I have a list 'Users' with a column 'UsersToAdd' of Type "Person or Group" which I like to use for provisioning a SP group 'MyUsers' at the creating of a new site.
I have created a feature receiver for this in my Custom Web Template.
When trying to deploy the feature I get an error on the SPFieldUserValue element:
Object reference not set to an instance of an object
Does anyone have an idea how to refactor this code for use in a Feature receiver? (It works when using it as code behind in a Custom App Page)
int ListID; //ID of item containing the users
string listUrl = string.Format("{0}/Lists/Users", sweb.Url);
SPList list = sweb.GetList(listUrl);
SPListItem item = list.GetItemById(ListID);
SPFieldUserValueCollection Users = (SPFieldUserValueCollection)item["UsersToAdd"];
cweb.AllowUnsafeUpdates = true;
foreach (SPFieldUserValue fuv in Users)
{
cweb.EnsureUser(@fuv.User.LoginName);
cweb.SiteGroups[MyUsers].AddUser(@fuv.User.LoginName, string.Empty, fuv.LookupValue, string.Empty);
}
cweb.AllowUnsafeUpdates = false;