Hi All,
In sharepoint A user account Information can be fetched out on the fly like Name,Picture,Attachments,About Me etc etc.
In a requirement of my Project i need to find the attachment of the Sharepoint User.
First of all let me know you all that when a user is created Sharepoint internally creates a List called as "User Information List".From this list we can get everything related to SharePoint User.
So here i go with the code snippet:
SPSite objSite = new SPSite(http://YourServerName/YourSiteName);
SPWeb objWeb = objSite.OpenWeb();
SPUser spusr = objWeb.CurrentUser;
SPList list = objWeb.SiteUserInfoList;
SPUserCollection users = objWeb.SiteUsers;
objWeb.AllowUnsafeUpdates = true;
string userId = Request.QueryString["UserId"].ToString();
string userName = "";
foreach (SPUser us in users)
{
if (userId == us.ID.ToString())
{
userName = us.LoginName;
}
}
SPListItem LstItem = list.Items.GetItemById(Convert.ToInt32(userId));
Tempimage.ImageUrl = LstItem.Attachments.UrlPrefix + "/" + LstItem.Attachments[0].ToString() ;
Well the catch here is the Last 2 lines where u give the attachment(here a JPeg image) its source.TempImage is an ASP:Image control.
Similary if you want to update the Profile Picture of a SP User..here you may go like this.
Microsoft.SharePoint.SPList list = web.Lists["User Information List"];
Microsoft.SharePoint.SPUser u = web.SiteUsers["domain\user1"];
Microsoft.SharePoint.SPListItem it = list.Items.GetItemById(u.ID);
it["Picture"] =”http://sharepointApp/sites/wss/photos/abc.jpg”;
it.Update();
Also, you are able to add a custom field to user profile like this:
Microsoft.SharePoint.SPList list = web.Lists["User Information List"];if (!list.Fields.ContainsField("Favorites")){ list.Fields.Add("Favorites", SPFieldType.Text, false); list.Update();}
Do visit for more ideas:
http://www.zimmergren.net/archive/2008/06/25/sharepoints-hidden-user-list-user-information-list.aspx
http://furuknap.blogspot.com/2009/08/sharepoint-user-information-list.html
http://vspug.com/jimyang/2006/12/05/update-wss-3-0-user-profile-programmatically/
Hope it helps.
Thanks,
Nitin Sharma
Tuesday, January 19, 2010
Wednesday, January 13, 2010
Switch Sharepoint Current User programmatically
Hey All,
If you guys want to switch the SharePoint User time and again in your Application(sharepoint application),then this code will hopefully is for you:
protected void Page_Load(object sender, EventArgs e)
{
SPSite spsite = new SPSite("http://YourServer/YourSite");
SPWeb web = spsite.OpenWeb();
string ObjUsr = "YourServer/YourUserName";
string SITE = http://YourServer/YourSite;
SwitchUser(web, SITE, ObjUsr);
}
private void SwitchUser(SPWeb web, string siteStr, string user)
{
SPUserToken userToken = web.AllUsers[user].UserToken;
SPSite s = new SPSite(siteStr, userToken);
SPWeb w = s.OpenWeb();
}
The key here is SPUserToken class. When a new site instance is created ,the second parameter is the UserToken which is assigned.
So here : w.CurrentUser.Name is different from web.CurrentUser.Name
Thanks,
Nitin Sharma
If you guys want to switch the SharePoint User time and again in your Application(sharepoint application),then this code will hopefully is for you:
protected void Page_Load(object sender, EventArgs e)
{
SPSite spsite = new SPSite("http://YourServer/YourSite");
SPWeb web = spsite.OpenWeb();
string ObjUsr = "YourServer/YourUserName";
string SITE = http://YourServer/YourSite;
SwitchUser(web, SITE, ObjUsr);
}
private void SwitchUser(SPWeb web, string siteStr, string user)
{
SPUserToken userToken = web.AllUsers[user].UserToken;
SPSite s = new SPSite(siteStr, userToken);
SPWeb w = s.OpenWeb();
}
The key here is SPUserToken class. When a new site instance is created ,the second parameter is the UserToken which is assigned.
So here : w.CurrentUser.Name is different from web.CurrentUser.Name
Thanks,
Nitin Sharma
How to Detect/Find Page is Loaded
Hi All,
You my want to know sometimes that is the page loaded completely?
This simple javascript function can let you know:
var body = document.getElementsByTagName('BODY')[0];
if (body && body.readyState == 'loaded')
{
AfterLoad();
}
else
{
if (window.addEventListener)
{
window.addEventListener('load', AfterLoad, false);
}
else
{
window.attachEvent('onload', AfterLoad);
}
}
function AfterLoad()
{
alert("page loaded !!!");
}
Thats it...
Thanks,
Nitin Sharma
You my want to know sometimes that is the page loaded completely?
This simple javascript function can let you know:
var body = document.getElementsByTagName('BODY')[0];
if (body && body.readyState == 'loaded')
{
AfterLoad();
}
else
{
if (window.addEventListener)
{
window.addEventListener('load', AfterLoad, false);
}
else
{
window.attachEvent('onload', AfterLoad);
}
}
function AfterLoad()
{
alert("page loaded !!!");
}
Thats it...
Thanks,
Nitin Sharma
Wednesday, December 30, 2009
Creating Sharepoint Site Programmatically
Hello All,
Below code snippet shows how to create a sharepoint site Programmatically.
SPSite oSiteCollection = new SPSite("http://Your Server name/");
SPWeb oWebSite = oSiteCollection.OpenWeb();
oWebSite.AllowUnsafeUpdates = true;
SPWebCollection oSitesCollection = oWebSite.Webs;
SPWeb newWebSite = oSitesCollection.Add("MySPSite", "My SharePoint Site Created Through Code", "This is site created through code.", 1033, "STS#1", true, false);
Thats't it...your dynamic site is now ready..!
For reference of Site Templates refer below:
STS#0
Team Site
STS#1
Blank Site
STS#2
Document Workspace
MPS#0
Basic Meeting Workspace
MPS#1
Blank Meeting Workspace
MPS#2
Decision Meeting Workspace
MPS#3
Social Meeting Workspace
MPS#4
Multipage Meeting Workspace
WIKI#0
Wiki
BLOG#0
Blog
For Locale ID, refer :
http://msdn.microsoft.com/hi-in/goglobal/bb964664(en-us).aspx
Hope it helps...
Thanks,
Nitin Sharma
Below code snippet shows how to create a sharepoint site Programmatically.
SPSite oSiteCollection = new SPSite("http://Your Server name/");
SPWeb oWebSite = oSiteCollection.OpenWeb();
oWebSite.AllowUnsafeUpdates = true;
SPWebCollection oSitesCollection = oWebSite.Webs;
SPWeb newWebSite = oSitesCollection.Add("MySPSite", "My SharePoint Site Created Through Code", "This is site created through code.", 1033, "STS#1", true, false);
Thats't it...your dynamic site is now ready..!
For reference of Site Templates refer below:
STS#0
Team Site
STS#1
Blank Site
STS#2
Document Workspace
MPS#0
Basic Meeting Workspace
MPS#1
Blank Meeting Workspace
MPS#2
Decision Meeting Workspace
MPS#3
Social Meeting Workspace
MPS#4
Multipage Meeting Workspace
WIKI#0
Wiki
BLOG#0
Blog
For Locale ID, refer :
http://msdn.microsoft.com/hi-in/goglobal/bb964664(en-us).aspx
Hope it helps...
Thanks,
Nitin Sharma
Programmatically upload a file to a document library in Sharepoint
Hi All,
Below code snippet upload a Doc file(any document) into the document library of a sharepoint site programmatically.
SPSite site = new SPSite("http://YourServerName/YourSite/");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
if (FileUpload2.HasFile)
{
SPFolder myLibrary = web.Folders["YourDocumentLibrary"];
String Filename = Path.GetFileName(FileUpload2.PostedFile.FileName);
FileStream fs = File.OpenRead(FileUpload2.PostedFile.FileName);
SPFile spFile = myLibrary.Files.Add(Filename, fs, true);
myLibrary.Update();
}
Hope it helps you all Sharepoint guys...
Thanks,
Nitin Sharma
Below code snippet upload a Doc file(any document) into the document library of a sharepoint site programmatically.
SPSite site = new SPSite("http://YourServerName/YourSite/");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
if (FileUpload2.HasFile)
{
SPFolder myLibrary = web.Folders["YourDocumentLibrary"];
String Filename = Path.GetFileName(FileUpload2.PostedFile.FileName);
FileStream fs = File.OpenRead(FileUpload2.PostedFile.FileName);
SPFile spFile = myLibrary.Files.Add(Filename, fs, true);
myLibrary.Update();
}
Hope it helps you all Sharepoint guys...
Thanks,
Nitin Sharma
Subscribe to:
Posts (Atom)