.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Custom Actions to calculate Document Size in SharePoint

Posted By:Jean Paul       Posted Date: June 27, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this scenario we have explorer the Custom Actions feature of SharePoint and using it in the Site level, Library level and Item level.

In this scenario we are using the Custom Actions infrastructure of SharePoint 2010. 


Our aim is to provide user interface controls to display the Document Size at:
·  Site level Calculate Document Size for all Libraries
·  Library level Calculate Document Size for all Documents
·  Document level Calculate Document Size for selected Document


We can use Custom Actions to address the above scenario.

What is Custom Action?

Custom Actions are special purposed actions integrated into the SharePoint user interface.  A custom action could be:
·  A menu item in the Site Actions menu
·  A button in the ribbon tool bar
·  A menu item in the context menu of list or library item
Please find some custom action samples: (we are going to create these actions now)

Why we need Custom Action?

In the development life cycle using SharePoint, we may encounter scenarios where the user prefers a special user control for in a particular context.  Some of the examples are:
·  Calculate Document Size in site level, library level, item level
·  Convert a particular document to PDF file
·  Display button to open Bing Search
·  Display the Audit History on site level, list level, item level

How to create Custom Actions?

We can create custom actions using Visual Studio 2010 and SharePoint project templates.  The custom actions can be represented using XML Element files which are deployed as Features.  The associated images can also be included in the project and get deployed automatically.

Custom Action Element

We use the XML tag CustomAction to specify custom action elements like menu item, button etc.
The custom action element defines an extension to the User Interface.  A sample custom action element contains the following tags and values.

Following are the properties which we are interested in:

·  ID to provide a unique Identifier
·  Title to display the caption
·  Description to provide information on the UI element
·  ImageUrl to specify the relative image path
·  Sequence to specify the order of listing
·  Location to specify the container

Our Aim

Our aim is to create 3 custom actions as shown in the above image.  For starting with create a new Visual Studio 2010 > Empty SharePoint project.

Now your Solution Explorer looks like below:

Create Site Actions menu item

Now we can try creating the first Custom Action:
·  Site Document Size

On clicking this menu item an application page will be shown.  This page should display the total size of documents summing up all libraries and the items inside it.
To create the custom action, right click on the Solution Explorer and use the Add New Item and choose the Empty Element option.

Enter the name as SiteActionsMenuItem.  Replace the contents of the file with the following.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="SiteActionsMenuItem"
  Description="Displays the Total Documents Size of the Site"
  Title="Site Document Size"
  <UrlAction Url="/_layouts/CustomActionsExample/DocSizeInfoPage.aspx?scope=web"/>

The above Custom Action element specifies the following:

1.  Set the Location as Microsoft.SharePoint.StandardMenu. We need to specify the exact location for each custom action element.  Here we are specifying the Standard Menu which is displayed on clicking the Site Actions link. You can see the GroupId set as SiteActions.

2.  Set the Title as Site Document Size. The title is used to specify the caption of men item.

3.  Set the ImageUrl, Sequence. These properties we can explore later.

Create Ribbon button

For creating the ribbon button you can add a new Empty Element.  Set the name as LibraryRibbonButton.

Replace the XML content with following:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="LibraryRibbonButton" Location="CommandUI.Ribbon.ListView" RegistrationId="101" RegistrationType="List" Title="Library Document Size">
  <CommandUIDefinition Location="Ribbon.Documents.Share.Controls._children">
  <Button Id="DocSizeButton" Image32by32="/_layouts/images/CustomActionsExample/green.png" Command="DocSizeButton" Description="Displays the Total Documents Size of the Library" LabelText="Library Document Size" TemplateAlias="o2" 
  <CommandUIHandler Command="DocSizeButton" CommandAction="/_layouts/CustomActionsExample/DocSizeInfoPage.aspx?scope=library&ListId={ListId}" />

In order to make the above button visible only for Document Libraries we are using the Registration Id attribute as 101.  101 represent the template Id for Document types.  The CommandAction is pointing to an application page with ListId as parameter set through runtime.

Create Document menu item

Here we are going to add a new menu item to the document context menu.  This menu is called Edit Control Block.
To achieve this create a new Empty Element and set the name as DocumentMenuItem.

Replace the XML content with following:

<?xml version="1.0" encoding="utf-8"?>
Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  Title="Document Size"

The Location specifies the container in which the custom action should occur.  In this case it is EditControlBlock.  The ImageUrl is set to a file which we will be adding later.  The url contains the application page which process the request.

You can see that {ListId} and {ItemId} are the parameters resolved in the Runtime.  It should contain the List GUID and the Item Id when invoked.

Creating Application Page

Here we are using a common application page to process all the 3 requests.  The requests are differentiated using the query parameters.
Create a new Application Page and name it as DocSizeInfo.aspx

In the page load event invoke a RefreshData() method as following.

protected void Page_Load(object sender, EventArgs e)
  if (!IsPostBack)

private void RefreshData()
  if (this.Request.QueryString["scope"] != null)
  string scope = this.Request.QueryString["scope"];

  if (scope == "web")

   else if (scope == "library")

  else if (scope == "item")

 Modify the Application Page HTML code to include a Label control.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
  Document Size Information</h1>
  <asp:Label ID="InfoLabel" runat="server" Font-Size="Small" ForeColor="Blue">

asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
  Document Size Information

asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
  Document Size Information

Please note that I have updated the Title in various places to Document Size Information.  These updating are reflected in various places as shown below.

Application Pages & Site Pages

In this example we have used Application Pages.  The Application pages are residing in the File System and are not editable like the Site Pages.

Calculation Methods

Now we can try completing the calculation methods for document size.
Method 1

private void CalcDocSizeForWeb()
  long total = 0;
  int count = 0;

  foreach (SPList list in SPContext.Current.Web.Lists)
  if (list is SPDocumentLibrary)
  count += list.Items.Count;

  foreach (SPListItem item in list.Items)
   total += item.File.Length;

  InfoLabel.Text = "Number of Documents in Web: " + count.ToString() + "</br>Total Documents Size of Web: " + total.ToMBString();

Method 2

private void CalcDocSizeForLibrary()
  long total = 0;
  int count = 0;

  // ListId assigned by SharePoint runtime
  Guid listGuid = new Guid(this.Request.QueryString["ListId"]);
  SPList list = SPContext.Current.Web.Lists[listGuid];

  if (list != null)
  if (list is SPDocumentLibrary)
  count = list.Items.Count;
  foreach (SPListItem item in list.Items)
  total += item.File.Length;

  InfoLabel.Text = "Number of Documents in Library: " + count.ToString() + "</br>Total Documents Size of Library: " + total.ToMBString();

Method 3

private void CalcDocSizeForItem()
  // ListId assigned by SharePoint runtime
  Guid listGuid = new Guid(this.Request.QueryString["ListId"]);
  SPList list = SPContext.Current.Web.Lists[listGuid];

  if (list != null)
  int itemId = Int32.Parse(this.Request.QueryString["ItemId"].ToString());
  SPListItem item = list.GetItemById(itemId);
  if (item != null)
  InfoLabel.Text = "Documents Size: " + item.File.Length.ToMBString();

You can see that an Extension Method is used.  Create a class for the extension method and place the following code in it.

namespace CustomActionsExample
  public static class ExtensionMethods
  public static string ToMBString(this long size)
  double result = (double)(((double)size / 1024) / 1000);

  return string.Format("{0:0.00}", result) + " MB";

The above extension method ToMBString converts the long value to Mega Bytes and returns a formatted string.

Now you are ready with the Methods and try building the project.  Fix errors if there are any, and we can proceed to the next section to set Images for our custom actions.

Setting Images for Custom Actions

Setting the Images are little tricky.  We need to map them to the correct folder.  Copy the images from the source (or create 3 images of your own).
Right click on the solution and use the Add > SharePoint "Images" mapped folder.

This will create an Images folder with a sub folder inside it.  Now add the images into it.

You need to place the image files inside this folder.  The above folder is mapped to:

After deployment the above image files will be copied to the SharePoint Front End Server >  14 HIVE > Template > Images > CustomActionsExample folder

You can see the same folder inside IIS (Internet Information Services)
Open IIS  > Select SharePoint site > Expand _layouts folder

The _layouts folder is a Virtual folder which is resolving into the 14 HIVE folder of SharePoint.  You can try clicking the Explore button from the right side to see  the Images folder.

Pointing the Images from Custom Actions

Inisde the custom action file we are pointing to the image as shown below:

Site Actions Menu Item > ImageUrl="/_layouts/images/CustomActionsExample/red.png"
Library Ribbon Button > Image32by32="/_layouts/images/CustomActionsExample/green.png"
Document Menu Item > ImageUrl="/_layouts/images/CustomActionsExample/blue.png"


You can try deploying the solution.  Right click on the project and use the Deploy menu item.

Seeing in Action

Now going back to your site, you can see the custom actions there.  On clicking them you can see the result.  Please note that the result changes according to the context.

Site Actions > Custom Action

Library > Ribbon > Custom Action

Document > Custom Action

Creating Test Documents

To verify the tests you can create 2 libraries in the Site.  Each library can have a 2 one MB file inside it.
The results should be:
·  Site Document Size as 4 MB
·  Library Document Size as 2 MB
·  Document Size as 1 MB

Security Trimming Custom Actions

We can show or hide a custom action based on the user's permission level.  The attribute Rights can be used for this purpose.  It takes comma separated values and is treated as AND operation.
For example:

<CustomAction Rights="Approve,Read">

You can see a user permission level by going to the Site Actions > Site Permissions page.

The setting of Permission Levels using Rights attribute is referred as Security Trimming for Custom Actions.




In this scenario we have explorer the Custom Actions feature of SharePoint and using it in the Site level, Library level and Item level.
I hope this will give you good grounds for a more complex scenario in future.
The source code contains the example we have discussed.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend