Tuesday, November 23, 2010

SHAREPOINT SPUSER CLASS

Example of SPUSER Class

// add name space
using Microsoft.SharePoint;
using System.Security.Principal;

public partial class _Default : System.Web.UI.Page
{


    WindowsPrincipal wp;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {


        wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
     
        string login = wp.Identity.Name;
       

        //SPSite oSiteCollection = SPContext.Current.Site;

        SPSite oSiteCollection = new SPSite("http://sks-blr-sp02:46346");
       // using (SPWeb oWebsite = oSiteCollection.AllWebs["http://sks-blr-sp02:46346"])
        using (SPWeb oWebsite = oSiteCollection.OpenWeb())
        {
          //  SPUser oUser = oWebsite.AllUsers["User_Name"];

            SPUser spuser = SPContext.Current.Web.AllUsers[login];
            TextBox1.Text = spuser.Name;
            TextBox2.Text = spuser.Email;
            TextBox3.Text = spuser.LoginName;
       
             // spuser.Update();
        }



    }
}

More information:

EXISTING ERRORS :


// using (SPWeb oWebsite = oSiteCollection.AllWebs["http://sks-blr-sp02:46346"])
Solution:

You could use the following options to get the SPweb object:

Option 1:

SPSite mysitecollection = new SPSite("http://bhaskar-4o6b0zz:406");
SPWeb site = mysitecollection.AllWebs["Cricket"]; (if Cricket is the name of the web)

Option 2:

SPSite mysitecollection = new SPSite("http://bhaskar-4o6b0zz:406/sites/Cricket");
SPWeb site = mysitecollection.OpenWeb();

// spuser.Update();

Solution:

spWeb.AllowUnsafeUpdates = true;