Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

SharePoint 2010 – Custom Actions to calculate Document Size

Posted by Paul on September 20, 2012


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

Scenario

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

Solution

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


GroupId=SiteActions


Location=Microsoft.SharePoint.StandardMenu


ImageUrl=/_layouts/images/CustomActionsExample/red.png


Sequence=10>

<UrlAction
Url=/_layouts/CustomActionsExample/DocSizeInfoPage.aspx?scope=web/>

</CustomAction>

</Elements>

 

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>

<CommandUIExtension>

<CommandUIDefinitions>

<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


Sequence=93

/>

</CommandUIDefinition>

</CommandUIDefinitions>

<CommandUIHandlers>

<CommandUIHandler
Command=DocSizeButton
CommandAction=/_layouts/CustomActionsExample/DocSizeInfoPage.aspx?scope=library&amp;ListId={ListId} />

</CommandUIHandlers>

</CommandUIExtension>

</CustomAction>

</Elements>

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/>

<CustomAction


Id=DocumentMenuItem


RegistrationType=List


RegistrationId=101


Location=EditControlBlock


Sequence=301


Title=Document Size


ImageUrl=/_layouts/images/CustomActionsExample/blue.png>

<UrlAction


Url=/_layouts/CustomActionsExample/DocSizeInfoPage.aspx?scope=item&amp;ListId={ListId}&amp;Itemid={ItemId}“/>

</CustomAction>

</Elements>

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)

RefreshData();

}

 

private
void RefreshData()

{


if (this.Request.QueryString[“scope”] != null)

{


string scope = this.Request.QueryString[“scope”];

 


if (scope == “web”)

{

CalcDocSizeForWeb();

}

 


else
if (scope == “library”)

{

CalcDocSizeForLibrary();

}

 


else
if (scope == “item”)

{

CalcDocSizeForItem();

}

}

}


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

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


<h1>

Document Size Information</h1>


<asp:Label
ID=”InfoLabel” runat=”server” Font-Size=”Small” ForeColor=”Blue”>

</asp:Label>

</asp:Content>

 

<asp:Content
ID=”PageTitle” ContentPlaceHolderID=”PlaceHolderPageTitle” runat=”server”>

Document Size Information

</asp:Content>

 

<asp:Content
ID=”PageTitleInTitleArea” ContentPlaceHolderID=”PlaceHolderPageTitleInTitleArea”


runat=”server”>

Document Size Information

</asp:Content>

 

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

Deployment

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>

</CustomAction>

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.

 

References

http://tinyurl.com/sp2010-sactn

http://tinyurl.com/sp2010-caecm

http://tinyurl.com/sp2010-csact

Summary

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.

Advertisements

One Response to “SharePoint 2010 – Custom Actions to calculate Document Size”

  1. Huzzah! New update! You’ve clearly been working on your writing skills. Bravo 🙂 Thank you so much for the imagination of well – you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s