Monday, July 2, 2012

How to Create Custom Application Page in SharePoint 2010 to get List Owners

In my environment, This is one of a Frequently asked question: How do I find who has created a List or Library? oops, I don't find any direct way from SharePoint interface to know who has created a List or Library.

Lets develop a Application page, host it under Layouts folder and let the users access that page to get the list owners information.

How to get Who has Created a List/Library?

Here is how to Create a Custom Application Page for SharePoint 2010 using Visual Studio - Step by Step:

1. Create a Visual studio "Empty SharePoint Project", Give it a name, Lets say "ListOwner".

2. Add a New Item to the Project, choose "Application Page", Name it, Say "ListOwner.aspx"
how to create custom application page in sharepoint 2010

3. This will create the project structure as in the below screen.
how to create custom page in sharepoint 2010

4. Now, Lets add a grid to the page, to display all the Lists and its owners from the current site.
Go to the ListOwner.aspx page, locate the code block:

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
</asp:Content>

and add the SPGrid in between above code. So it will be:
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"><h2>List owners Info:</h2></asp:Label>
     
     <SharePoint:SPGridView runat="server" ID="GridView" AutoGenerateColumns="false"
            GridLines=Horizontal HeaderStyle-BackColor="#18518E" RowStyle-BackColor="#D3E4E5" RowStyle-ForeColor="Black" AlternatingRowStyle-BackColor="#fcfcfc" HeaderStyle-Font-Bold="true"  />
     
</asp:Content>

As the next step, we've to write the code to fetch the lists & libraries to the Grid, by editing ListOwner.aspx.cs

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI.WebControls;

namespace ListOwner.Layouts.ListOwner
{
    public partial class ListOwner : LayoutsPageBase
    {
   
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
      
            //Add columns to Data Table
            dt.Columns.Add("List Name", typeof(string));
            dt.Columns.Add("List URL", typeof(string));
            dt.Columns.Add("Description", typeof(string));
            dt.Columns.Add("Owner", typeof(string));
            dt.Columns.Add("OwnerURL", typeof(string));

            //Add List info as rows
            foreach(SPList list in SPContext.Current.Web.Lists)
            {
                //can Filter the Lists by SPListTemplateType http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx
                
                DataRow row = dt.NewRow();
                row["List Name"] = list.Title;
                row["List URL"] = SPContext.Current.Web.Url + list.RootFolder.ServerRelativeUrl;
                row["Description"] = list.Description;
                row["Owner"] = list.Author.LoginName;
                row["OwnerURL"] = SPContext.Current.Web.Url + "/_layouts/userdisp.aspx?ID=" + list.Author.ID + "&Source=" + Page.Request.Url;
       
                dt.Rows.Add(row);
            }

            //Add Columns to Grid
            HyperLinkField col1 = new HyperLinkField();
            col1.HeaderText = "List Name";
            col1.DataTextField = "List Name";
            col1.DataNavigateUrlFields = new string[] { dt.Columns["List URL"].ToString() };
            GridView.Columns.Add(col1);

            SPBoundField col2= new SPBoundField();
            col2.HeaderText = "Description";
            col2.DataField = "Description";
            GridView.Columns.Add(col2);

            HyperLinkField col3 = new HyperLinkField();
            col3.HeaderText = "List Owner";
            col3.DataTextField = "Owner";
            col3.DataNavigateUrlFields = new String[] { dt.Columns["OwnerURL"].ToString() };
            GridView.Columns.Add(col3);
                 
            //Set the Datasource and bind to Grid
            GridView.DataSource = dt;
            GridView.DataBind();

        }
    }
}

Build and deploy the project! That's all, we are done creating custom SharePoint 2010 application page using visual studio 2010.
how to create custom sharepoint 2010 application page using visual studio 2010

Tail: 
How to find Who has Created a Particular SharePoint Site collection or Sub-Site?
You can get the Site Creator, Created Time, etc by accessing the SPWeb.Author, SPWeb.Created properties.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


Check out these SharePoint products:

3 comments :

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Is there a way to do this same thing using SharePoint designer?

    ReplyDelete
    Replies
    1. I believe No! However, If you don't want to use Server side code, you can try it with Client Side Object Model to achieve the same!

      Delete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...