How to get current Pages details in SharePoint2010 publishing site

Hi friends,

Through this blog I just want to share my experince that how to recognize your current page or can get page detail for your current from sharepoint page library.

You need to use the below code:

char[] delimiters = new char[] { '\\', '?' };
string s = Page.Request.Path.ToString();
string[] words = s.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
int i = words[0].IndexOf("Pages/");
foreach (SPListItem uitem in SPContext.Current.Web.Lists["Pages"].Items)
{
       if (uitem["FileLeafRef"].ToString().ToLower()==words[0].Substring(i + 6).ToLower())
       {
            //you code logic to get item value e.g uitem["Title"].ToString() etc
        }
}
Explaination:

1.I am assuming my current page url is :http://sharepointHome:2012/Pages/Sample.aspx?id......(blahh blahh...with some query string).

2.Now define delimiter and string to get current page url:

      char[] delimiters = new char[] { '\\', '?' };
      and 
      string s = Page.Request.Path.ToString();
Output of s will be:
http://sharepointHome:2012/Pages/Sample.aspx?id......(blahh blahh...with some query string).

3.Create one string of array and use integer to get value for IndexOf .

      string[] words = s.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
      int i = words[0].IndexOf("Pages/");
 
 Output of words[0].Substring(i + 6) :

"Sample.aspx" which is equal to  SPContext.Current.Web.Lists["Pages"].Items["FileLeafRef"]

Hope you like this blog.It will reduce your extra effort.

Please post me if you have any doubt :)

Ebook Download
View all
Learn
View all