Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, December 01, 2011

How to Use SpUserToken in SharePoint 2007 or SharePoint 2010



using System; using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.Security;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SPUserToken sToken = SPContext.Current.Site.SystemAccount.UserToken;
using (SPSite site = new SPSite(http://vlnrm:100/, sToken))
{
using (SPWeb objWeb = site.OpenWeb())
{
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
SPList lst = objWeb.Lists["My Links"];
SPListItemCollection myColl = lst.GetItems(query);
if (myColl.Count > 0)
{
this.Controls.Add(new LiteralControl("<Table width='100%' bgcolor='Aqua'>"));
this.Controls.Add(new LiteralControl("<Tr>"));
this.Controls.Add(new LiteralControl("<Td>"));
this.Controls.Add(new LiteralControl("<Strong>"));
this.Controls.Add(new LiteralControl("Link Name"));
this.Controls.Add(new LiteralControl("</Strong>"));
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("</Tr>"));
foreach (SPListItem item in myColl)
{
string strHyperLInkTitle = string.Empty;
string strHyperLinkURL = string.Empty;
strHyperLInkTitle = item["HyperLinkTitle"].ToString();
strHyperLinkURL = item["URL"].ToString();
this.Controls.Add(new LiteralControl("<Tr>"));
this.Controls.Add(new LiteralControl("<Td>"));
this.Controls.Add(new LiteralControl("<A href='"+strHyperLinkURL+"'>"+strHyperLInkTitle+"</A>"));
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("</Tr>"));
}
this.Controls.Add(new LiteralControl("<Table>"));
}
}
}
}
}

No comments:

Post a Comment