Monday, October 17, 2011

Get User Login Name from SP User/Group Column in Sharepoint

SPFieldUser field = item.Fields["Approver"] as SPFieldUser;

if (field != null)
{
SPFieldUserValue fieldValue = field.GetFieldValue(item["Approver"].ToString()) as SPFieldUserValue;
if (fieldValue != null)
{
string str= fieldValue.User.LoginName;
}

}

Saturday, October 1, 2011

How to display all versions in a SharePoint list view page

http://stefan-stanev-sharepoint-blog.blogspot.com/2009/08/how-to-display-all-versions-in.html

Programmatically get custom list and document library Version History

private void GetVersionHistory()
{
try
{
using (SPSite site = new SPSite(SPContext.Current.Site.Url.ToString()))
{
using (Microsoft.SharePoint.SPWeb web = site.OpenWeb())
{
Guid id = new Guid(this.Request.QueryString["ListId"].ToString());
SPList objList = web.Lists[id];
DataTable dtPhoto = new DataTable("dtPhoto");
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;

if (objList.BaseType == SPBaseType.DocumentLibrary)
{
char[] sep = { '' };
char[] sepHash = { '#' };
SPListItem item = objList.GetItemById(Convert.ToInt32( this.Request.QueryString["ItemId"].ToString()));
string strname = item["Name"].ToString();
foreach (SPFile file in objList.RootFolder.Files)
{
if (file.Name == strname)
{
dtPhoto.Columns.Add("Version");
dtPhoto.Columns.Add("Modified");

dtPhoto.Columns.Add("Modified By");
dtPhoto.Columns.Add("Comments");
DataRow dtrow;

dtrow = dtPhoto.NewRow();
dtrow["Version"] = file.UIVersionLabel;
dtrow["Modified"] = item["Modified"].ToString();

string[] str1 = item["Modified By"].ToString().ToString().Split(sepHash);

dtrow["Modified By"] = ( str1[1].ToString());
dtrow["Comments"] = file.CheckInComment;

dtPhoto.Rows.Add(dtrow);

SPFileVersionCollection filecollection = file.Versions;
if (filecollection.Count > 0)
{



foreach (SPFileVersion v in filecollection)
{

dtrow = dtPhoto.NewRow();
dtrow["Version"] = v.VersionLabel;
dtrow["Modified"] = v.Created;
string[] str = v.CreatedBy.ToString().Split(sep);

dtrow["Modified By"] =GetUserName( str[1].ToString());
dtrow["Comments"] = v.CheckInComment;

dtPhoto.Rows.Add(dtrow);

}
if (dtPhoto != null && dtPhoto.Rows.Count > 0)
{
gvVersion.DataSource = dtPhoto;
gvVersion.DataBind();


}
else
{
trversion.Visible = false;
gvVersion.Visible = false;

}
return;
}
}
}
}
else
{

#region


SPQuery objQuery = new SPQuery();
objQuery.Query = "<Where><Eq><FieldRef Name=\"ID\"/><Value Type=\"Counter\">" + this.Request.QueryString["ItemId"].ToString() + "</Value></Eq></Where>";
SPListItemCollection objItemColl = objList.GetItems(objQuery);


foreach (SPListItem objItem in objItemColl)
{
SPListItemVersionCollection objVerisionColl = objItem.Versions;
if (objVerisionColl.Count > 0)
{
dtPhoto.Columns.Add("Version");
dtPhoto.Columns.Add("Modified");
dtPhoto.Columns.Add("Modified By");
dtPhoto.Columns.Add("Comments");

DataRow dtrow;


//Navigate to each version of the ListItem
foreach (SPListItemVersion objVersion in objVerisionColl)
{
SPListItem objLstItm = objVersion.ListItem;
char[] sep = { ',' };
char[] sep1 = { ';' };
dtrow = dtPhoto.NewRow();
dtrow["Version"] = objVersion.VersionLabel;
dtrow["Modified"] = objLstItm.Versions.GetVersionFromLabel(objVersion.VersionLabel)["Modified"].ToString();
string[] strtest = objLstItm.Versions.GetVersionFromLabel(objVersion.VersionLabel)["Modified By"].ToString().Split(sep);
string[] str11 = strtest[0].Split(sep1);
string s = str11[1].ToString().Replace("#", "");
dtrow["Modified By"] = s;
dtrow["Comments"] = "";

dtPhoto.Rows.Add(dtrow);
}
}
}
#endregion

}


if (dtPhoto != null && dtPhoto.Rows.Count > 0)
{
gvVersion.DataSource = dtPhoto;
gvVersion.DataBind();


}
else
{
trversion.Visible = false;
gvVersion.Visible = false;

}



}
}
}
catch (Exception)
{


}
}

Thursday, July 14, 2011

Show username instead of "System Account" in SharePoint

Many times when you login to a SharePoint site you will see at the right hand side top corner "Welcome System Account" instead of your username. Sometimes we need to really get rid of this system account and display proper username.
Here are the two ways which could help you accomplish this task:
Solution one :
· Go to Central Administration / Application Management.
· Click Policy for Web Application.
· Select your web app and click your account in the list.
· In the Edit Users page, clear the Account operates as System checkbox.
· That should fix this particular problem.
Solution two:
· Go to Central Administration / Operations.
· Select Service Accounts
. Select your web application
· Change the application pool identity for your Web application

Sunday, June 12, 2011

SharePoint 2010 - i:0#.w|domain\username issue in User

Hi Friend,

Yesterday When I fetched user name through object model, I am getting user name like i:0#.w|domain\username. Because It is possible in claim based authentication in sharepoint.

To Solve this problem, decode the account name.

using Microsoft.SharePoint.Administration.Claims;
SPClaimProviderManager mgr = SPClaimProviderManager.Local;
string strDecodedLoginName = "";


strDecodedLoginName = mgr.DecodeClaim(strLoginName).Value;