Using SPUser in SharePoint
SharePoint Object Model using SPUser Class
SPUser is actually derived from SPPrinciple, SPUser is main
base class which allows in extending its functionality by exposing its member
and different properties.SharePoint object model has provided the provision to
add the user programatically to sharepoint group or permissions to user to a list,and it is possible to update the user information or fetch the user information.
The following sample code shows about how to display user
permission is he in admin group or normal user
also it has the code to update the user information or giving the permission to a list
WebPart
using System;
using System.ComponentModel;
using System.Web;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace SPUserDemo
{
[ToolboxItemAttribute(false)]
public class SPUserDemoWebPart:
WebPart
{
protected override void CreateChildControls()
{
}
protected override void RenderContents(HtmlTextWriter
writer)
{
//Get the user
information
GetUser(writer)
}
private void GetUser(HtmlTextWriter writer)
{
SPUser SPuser = SPContext.Current.Web.CurrentUser;
if(SPuser.IsSiteAdmin)
writer.Write("<p>Ohh you are Super admin user'</p>");
else
writer.Write("<p>No you are a normal user </p>");
}
}
}
Private void AddNewUser()
{
const string listUsers = @"Users ";
SPList Users= null;
SPSecurity.RunWithElevatedPrivileges(
delegate {
using (SPSite spSsite= new SPSite(“http://sitedemo”)) {
using (SPWeb spWeb= spSsite.OpenWeb)
{
spWeb.AllowUnsafeUpdates = true;
Guid listGuidID = spWeb.Lists.Add listUsers,
"Members", SPListTemplateType.GenericList);
Users = spWeb.Lists[listGuidID];
Users.Fields.Add("User", SPFieldType.User, true);
Users.WriteSecurity = 4;
Users.Update();
spWeb.Update();
}
}
});
}
//update user email information
using (SPSite spSsite=new SPSite(“http://sitedemo”)) {
using (SPWeb spWeb= spSsite.OpenWeb)
{
SPUser spUser = spWeb.AllUsers["ABCUser"];
spUser.Email =
" E-mail_Address";
spUser.Update();
}
}
No comments:
Post a Comment