Extending ECB by using JavaScript

Within this article I’d like to show you how to extend the ECB of a simple list, by using the JavaScript API which is offered by SharePoint.
Page Content

 

The EditControlBlock (ECB) is one of the commonly used elements in SharePoint. Almost every SharePoint developer has added a command to the ECB by using a CustomAction. But when you try to add submenus or just a simple separator you aren’t able to achieve this by using the default mechanisms such as CAML or CLR Code. You have to do it in JavaScript.

Within this article I’d like to show you how to extend the ECB of a simple list, by using the JavaScript API which is offered by SharePoint.

First let’s create an empty SharePoint 2010 project in VS 2010.

2011-09-03-ExtendingECB-01.png Figure 1 : Creating a new SharePoint Project in VS2010

SharePoint offers various ways, to ensure that our custom JavaScript code is executed when the required site is requested. In my opinion, using a SharePoint DelegateControl would be the cleanest one. So, create a new UserControl, the ECB_Extender.ascx, which is really easy using the SharePoint 2010 ItemTemplates shipped with Visual Studio 2010.

2011-09-03-ExtendingECB-02.png Figure 2: Adding a UserControl to the SharePoint Project

In addition to our UserControl, we need an ElementFile to register the ECB_Extender.ascx for the AdditionalPageHead placeholder. The “Empty Element” ItemTemplate from VisualStudio 2010 or the “DelegateControl” ItemTemplate from CKSDev are the right decision.

2011-09-03-ExtendingECB-03.png Figure 3: Creating a new elementFile

Ok, so that’s our final project structure. More isn’t required to extend the ECB on any list or library by using JavaScript code.

Your entire solution structure should look similar to figure 4

2011-09-03-ExtendingECB-04.png Figure 4: Final Solution Structure

Registering the UserControl

Let’s register the ECB_Extender.ascx for the AdditionalPageHead. Really easy by using CAML.

1 <!-- (\DelegateControls\Elements.xml) -->
2
3 <?xml version="1.0" encoding="utf-8"?> 
5 <Control Id="AdditionalPageHead"
6 ControlSrc="~/_ControlTemplates/ECB_Extensions/ECB_Extender.ascx"/>
7 </Elements>

Extending the ECB

When you’re going to extend a list you could use the method Custom_AddListMenuItems(m,ctx) for your extensions. The ECB’s default processing pipeline is looking for this method and executes it, when it’s defined.

When you’re extending a document library on the other hand, you’ve to implement the JavaScript method Custom_AddDocLibMenuItems(m,ctx).

As you can see both methods accept the same parameters, which is the entire ECB menu and ctx, which is the current execution context.

The context parameter is especially useful for conditional rendering or extending the EditControlBlock.

For a better understanding, let’s create both method bodies within the ECB_Extender’s markup

01 <script type="text/javascript"
02
03 // use this method to extend ECB on common lists...
04 function Custom_AddListMenuItems(m, ctx) { 
05
06
07 // use this method to extend ECB on libraries...
08 function Custom_AddDocLibMenuItems(m, ctx) { 
09
10
11
12 </script>

ECB Extension Methods

Finally, we have to look for some way to get our items into the ECB. SharePoint offers three different extension approaches for ECB; you can add simple commands, submenus and separators.

To create a new command and add it to the ECB you have to call the CAMOpt() method. To create new separators you should use CAMSep(), and CASubM() for creating new submenus.

When you take a closer look at the method signatures, you’ll find the parent parameter in each of them. While adding items to the ECB you just pass the ECB instance (m) itself. When adding items to a submenu you have to pass the submenu instance as parent element. I think all other parameters are really self-explainatory.

01 function CAMOpt(parent,caption,action,imageSrc,imageAlt,sequence,description) 
02  
03 {
04 //…
05 }
06 function CAMSep(parent)
07 {
08 //..
09 }
10  
11 function CASubM(parent, caption,imageSource,imageAlt,sequence,description)
12 {
13 //..
14 }

The final script to extend the ECB on any list is really simple.

01 <script type="text/javascript">
02 // use this method to extend ECB on common lists...
03 function Custom_AddListMenuItems(m, ctx) {
04 // Adding a simple command to the ECB
05 CAMOpt(m, 'Go to MSDN', 'javascript:STSNavigate("http://www.msdn.microsoft.com");return false;'); 
06
07 // Adding a separator to the ECB
08 CAMSep(m);
09  
10 // Adding commands to a submenu
11 var subItem = CASubM(m, 'Open MSDN');
12 CAMOpt(subItem, "SharePoint Developer Center", 'javascript:STSNavigate("http://msdn.microsoft.com/en-us/sharepoint/aa905688");return false;');
13 CAMOpt(subItem, "Azure Developer Center", 'javascript:STSNavigate("http://msdn.microsoft.com/en-us/windowsazure/cc947856");return false;');
14
15
16 // use this method to extend ECB on libraries...
17 function Custom_AddDocLibMenuItems(m, ctx) {
18  
19
20
21 </script>

2011-09-03-ExtendingECB-05.png Figure 5: Simple Command with Separator

Figure 5 shows the command “Go to MSDN” with the separator and in figure 6 you can see the MSDN submenu.

2011-09-03-ExtendingECB-06.png Figure 6: SubMenu

Conditional extensions

If you’d like to extend only a specified list, you can use the ctx parameter and check for the list like I did in this sample

1 function Custom_AddListMenuItems(m,ctx){
2  
3 if (ctx.listName.toUpperCase() == '{6f090b15-698f-4920-bba9-97c07cd41b14}'.toUpperCase()) {
4 CAMOpt(m, 'Add new subitem', action, ‘/_layouts/images/ECB_Extensions/myIcon.png’);
5 CAMSep(m);
6
7 }

Conclusion

Sure, extending ECB by using CustomActions is easier and enough for the most situations, but the extension way using JavaScript is also very nice and really easy. Since the raise of SharePoint 2010 each SharePoint Developer is doing more JavaScript than before. So I think ECB is another area where you can reuse your JavaScript skills or it’s also a good point to get started with JavaScript for SharePoint 2010. Happy SharePoint’ing.

Advertisements

User Profile service application overview (SharePoint Server 2010)

Uses and benefits of the User Profile service application

The User Profile service application in Microsoft SharePoint Server 2010 provides a central location where service administrators configure and manage the following features:

  • User profiles – contain detailed information about individuals in an organization. A user profile organizes and displays all of the properties related to each user together with social tags, documents and other items related to that user.
  • Organization profiles – contain detailed information about an organization such as teams, divisions, and so on.
  • Profile synchronization – provides a reliable way to synchronize groups and user profile information that is stored in the SharePoint Server 2010 profile store with information that is stored in directory services across the enterprise.
  • Audiences – enables organizations to target content to users based on their job or task, as defined by their membership in a SharePoint Server group or distribution list, by the organizational reporting structure, or by the public properties in their user profiles.
  • My Sites Host – a dedicated site for hosting My Sites. A My Sites Host is needed in order to deploy the social features of SharePoint Server.
  • My Site – a personal site that gives users in your organization a central location to manage and store documents, links, and colleagues.
  • Social tags and notes – enables users to add social tags to documents, to other SharePoint Server items, and to other items, such as external Web pages and blog posts. Users can also leave impromptu notes on profile pages of a My Site or any SharePoint Server page. Administrators can delete all tags for employees when they leave the company or remove a tag they do not want.

These features make it possible for users in an organization to share information and to stay informed about what is happening within the organization. Social tags, for example, enable users to tag and track the information they are most interested in. Users can be alerted when people they work with author new blog posts or when there is a change in organizational metadata.

Like other service applications in SharePoint Server 2010, the User Profile service application is easy to deploy and set up. Farm administrators can delegate the management of all or part of the features of the User Profile service application to one or more service application administrators. For more information, see Assign or remove administrators to a service application (SharePoint Server 2010).

Architecture

When you create a User Profile service application, SharePoint Server creates three databases for storing user profile information and associated data:

  • a profile database – used to store user profile information.
  • a synchronization database – used to store configuration and staging information for synchronizing profile data from external sources such as the Active Directory Domain Services (AD DS).
  • a social tagging database – used to store social tags and notes created by users. Each social tag and note is associated with a profile ID.

Each of these databases can be accessed by My Sites, Team Sites, and other SharePoint sites by using the User Profile service application. This provides a dynamic, personalized experience for the users in an organization. For more information about these databases, see Databases used by SharePoint 2010 Products.

The User Profile service application can be managed by the appropriate business group. One administrator can manage all areas of the User Profile service application. As an alternative, areas can be isolated and managed by different administrators who need not know about the existence of other areas of the service application. For example, one administrator can manage My Sites while a different administrator manages social tags and notes. The User Profile service application can be restricted and available only to certain departments or sets of sites based on business need, security restrictions, and budgets. For more information about the services architecture in SharePoint Server, see Logical architecture components (SharePoint Server 2010).

Related service applications

The User Profile service application relies on other service applications to implement the full range of social computing features in SharePoint Server. These related service applications include the following:

The information contained in this electronic communication is intended solely for the individual(s) or entity to which it is addressed. It may contain proprietary, confidential and/or legally privileged information. Any review, retransmission, dissemination, printing, copying or other use of, or taking any action in reliance on the contents of this information by person(s) or entities other than the intended recipient is strictly prohibited and may be unlawful. If you have received this communication in error, please notify us by responding to this email or telephone and immediately and permanently delete all copies of this message and any attachments from your system(s). The contents of this message do not necessarily represent the views or policies of Aditya Birla Group. Computer viruses can be transmitted via email. Aditya Birla Group Companies attempts to sweep e-mails and attachments for viruses, it does not guarantee that either are virus free. The recipient should check this email and any attachments for the presence of viruses. Aditya Birla Group does not accept any liability for any damage sustained as a result of viruses.