Thursday, August 5, 2010

Checking whether the User is valid Sharepoint User or not

Hi All,
I have been working quite on some sharepoint stuff and faced a scenario of ensuring whether a particular user is valid or not either with the sam account name or with the complete name.
So I have used the below statement in my code and then ensured the user is valid or not.It ensures that the user is present in Active Directory.
SPUser assignedUser = web.EnsureUser("");
Using this we would be able to update the columns like "Created By", "Assigned To" and so on where they are either people picker columns or people columns.
Just what you must do is create an SPUser object and then ensure it using the web (where web is the object for SPWeb) then assign this to the column of the list which is intended for.
Note: I even have faced the issue where the EnsureUser is not working with the web, so seriously what i have done is i have taken a new web object with the spSiteColl.RootWeb() then my problem got resolved.
Ref Code:
  1. SPSite site = new SPSite();
  2. SPWeb web = site.RootWeb();
  3. SPUser assignedUser = web.EnsureUser("");
  4. web.Dispose();
  5. site.Dispose();
This resolved my problem.

No comments:

Post a Comment