0

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;
2
  • Is the third line really right? You're getting the item using the ListID? IF it isn't, that could be your problem. What populates ListID?
    – Andy Burns
    Feb 4, 2011 at 9:27
  • Sorry should have mentioned that ListID is and int defined above. So the correct item is used... Problem is at line foreach (SPFieldUserValue fuv in Users) Seams Feature Receiver has issues with object SPFieldUserValue
    – Wim Hill
    Feb 4, 2011 at 9:57

2 Answers 2

0

There is nothing wrong with your code as far as I can see. Therefor I tested your code and the time I get the error you described is when the field "UsersToAdd" in the list is empty(doesn't contain any value). A check for null on "Users" will prevent you from getting the error.

Regards, Anita

1
  • Hi Anita, Thanks for your update on this... I'm going to do some more debugging today taking your remarks into account!
    – Wim Hill
    Feb 7, 2011 at 11:31
0

Yup, I reckon the third line should be:

SPListItem item = list.GetItemById(ListItemID);

Also, note that you can attach Visual Studio to the W3WP process for debugging - I find that very useful in event handlers.

3
  • Sorry should have mentioned that ListID is and int defined above. So the correct item is used... Problem is at line foreach (SPFieldUserValue fuv in Users) Seams Feature Receiver has issues with object SPFieldUserValue
    – Wim Hill
    Feb 4, 2011 at 9:58
  • Do you have a SharePoint group in one of the fields? I'm not sure that they'd have a LoginName - or even a User object.
    – Andy Burns
    Feb 4, 2011 at 13:42
  • No SharePoint Groups are passed, only single users... Just to make sure the code works fine as code behind of a Custom App Page, it only throws an error in VS2010 when I try to use it in my feature Receiver code... Mainly the use of SPFieldUserValue.. Following post does it in a slightly diffrent way (also feature receiver code) sharepointnutsandbolts.com/2007/08/…
    – Wim Hill
    Feb 4, 2011 at 14:02

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.