0

I have created a custom list in MOSS 2010 that has a column type of Person/Group. I am storing AD username in that column. Also, I have created a view for the list that is filtering data based on the Person/Group column. All is good if I add/edit items in the list via MOSS.

However, I have created a web part that inserts the items in this list. After inserting the items in the list, the view does not display the items inserted to the list via webpart . However, if I edit the inserted record and save it again, it displays the record in the custom view.

Can you please help in this as I have wasted almost a day in fixing this and tried and tested all the obvious errors that I could like, checking of spaces/special charcters etc.

6
  • "view does not display the items correctly" - what does that mean? Commented Jan 12, 2012 at 16:34
  • @AshishPatel..I mean to say, it does not show the records at all in the custom view.I have edited the question Commented Jan 12, 2012 at 16:46
  • 1
    Can you share code snippet where you create list item? also when item is not displayed, how could you edit again? what's the criteria for the custom view? Commented Jan 12, 2012 at 16:51
  • I am editing the list item, by going in the All Items View of the list. Custom view displays the items where the Person/Group column = Current logged in user. Hope this makes it clear Commented Jan 12, 2012 at 16:52
  • Sorry, but would not be able to share code :( Commented Jan 12, 2012 at 17:58

1 Answer 1

1

I can't say exactly (because you didn't provide any code), but it looks like that you're putting some loose values into your Person/Group column. When you re-save item, SharePoint resolves the issue, probably by re-parsing the value.

The point is, that actually SharePoint validation is occasionally not very good and sometimes you're able to set some incorrect (bad-formatted) values through SharePoint Object Model.

Please, review your code intently, paying particular attention to the fragment where the Person/Group column is getting filled.

The right scenario for setting up a Person/Group field value will be:

  1. First of all, you need to retrieve ID of the user if you have his login name, the best option will be to use SPWeb.EnsureUser method (it searches for the user, and if user with specified login was not found, it adds him to site).
  2. Having user ID, and having SPListItem object instance, you should set the field value. The most prudent way to retrieve the right field is to use the field's guid.

The code will look something like this:

var loginName = // pass user login name here
var fieldGuid = // pass your field Guid here

var userId = SPWeb.EnsureUser(loginName).ID;
listItem[fieldGuid] = userId;
// ... set other columns
listItem.Update();
5
  • Thanks for posting the detailed response. I agree with you and confirm I am using the same approach as mentioned by you. The only difference is only in this line: var userId = SPWeb.EnsureUser(loginName); listItem[fieldGuid] = userId; Commented Jan 12, 2012 at 18:51
  • I just tried changing var userId = SPWeb.EnsureUser(loginName); to var userId = SPWeb.EnsureUser(loginName).ID; but it is still not working :( Commented Jan 12, 2012 at 19:06
  • Then, the only approach I can recommend, is to use PowerShell or some simple console program to dump all the values from a problematic list item. So, you can run the program before and after re-saving the item, and then you will be able to compare the two dumps and find out what has changed. Commented Jan 12, 2012 at 19:24
  • ok..will try it now! Commented Jan 12, 2012 at 19:37
  • any example on how to dump the records from PS wud be gr8! Commented Jan 12, 2012 at 20:12

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.