Custom Actions to calculate Document Size in SharePoint

Posted By:Jean PaulPosted Date:June 27, 2013Points:200
Category: SharePointURL:http://www.dotnetspark.comIn 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.

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 itemPlease 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.

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.

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.

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

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:

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.

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.