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 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;
share|improve this question
    
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 '11 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 '11 at 9:57
add comment

2 Answers

up vote 0 down vote accepted

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

share|improve this answer
    
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 '11 at 11:31
add comment

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.

share|improve this answer
    
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 '11 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 '11 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 '11 at 14:02
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.