Thursday, May 26, 2011

Programmatically Add Custom Action ( Link ) in ECB menu of an List Item specific to a Content Type and open Application Page in Modal Dailog Popup in Sharepoint 2010

Many a times we are having a requirement like we have to add a Link in ECB menu of an Item and open an application Page in a modal dialog pop up box of Sharepoint 2010.
In this article we will see how
·         Programmatically we can add a link in ECB and associate it with  a Content Type on Feature Activation.
·         Open an Application Page on the click of the ECB link in Modal Dailog Box of SharePoint.
·         Remove the link on Feature Deactivation.
Below are the One by One Steps:
1.        Open Visual Studio 2010 and create an Empty Sharepoint 2010 Project and add a Feature Reciever on it.
2.       On FeatureActivated method write the below given code.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPContentType objContentType;
            //SPSite objSPSite;
            //SPWeb objSPWeb;
            string sUrl;

            sUrl = ((SPSite)properties.Feature.Parent).Url;

            using (SPSite objSPSite = new SPSite(sUrl))
            {
                using (SPWeb objSPWeb = objSPSite.OpenWeb())
                {
                    objContentType = objSPWeb.ContentTypes["TestContentLink"];

                    SPUserCustomAction action = objSPWeb.UserCustomActions.Add();
                    action.RegistrationType = SPUserCustomActionRegistrationType.ContentType;
                    action.RegistrationId = objContentType.Id.ToString();
                    action.Location = "EditControlBlock";
                    action.Sequence = 450;
                    action.Title = "New Configuration Link";
                    action.Rights = SPBasePermissions.EditListItems;
                    //action.Url = "/_Layouts/TestContentTypeLinkSettings/NewConfigurationWizard.aspx";
                    action.Url = "javascript:OpenPopUpPageWithTitle('{SiteUrl}/_Layouts/TestContentTypeLinkSettings/NewConfigurationWizard.aspx', RefreshOnDialogClose, 600, 400,'My Custom PopUp')";
                    action.Update();

                }
            }

           
        }

This code snippet will register the “New Configuration Link” in the ECB menu of the List Item created with the content type “TestContentLink


1.       Add one application Page in your Project.
action.Url = "javascript:OpenPopUpPageWithTitle('{SiteUrl}/_Layouts/TestContentTypeLinkSettings/NewConfigurationWizard.aspx', RefreshOnDialogClose, 600, 400,'My Custom PopUp')";

This Url will be the URL of the application page which you have created. This JavaScript will open the newly added custom page in the Modal Dailog popup.
2.       On feature deactivation we have to remove this link also. To remove the link add the below mentioned Code in FeatureDeactivating method:
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            string sUrl;

            sUrl = ((SPSite)properties.Feature.Parent).Url;

            using (SPSite objSPSite = new SPSite(sUrl))
            {
                using (SPWeb objSPWeb = objSPSite.OpenWeb())
                {
                    foreach (SPUserCustomAction action in objSPWeb.UserCustomActions)
                    {
                        SPContentType objContentType = objSPWeb.ContentTypes["TestContentLink"];
                        if (action.RegistrationId == objContentType.Id.ToString())
                        //if (action.Name != null && action.Name.Equals("Test"))
                        {
                            action.Delete();
                            objSPWeb.Update();
                            break;
                        }
                    }
                }
            }
        }
We have the Registration ID of the ECB link which we specified on Feature Activated method. Based upon that Registration ID we are removing that custom action.

In this way we can add an Custom Action/Link in ECB menu of the Sharepoint ListItem specific to a Content Type.

 
And on clicking this link it will open the application page created in Modal Dialog Popup of Sharepoint 2010.

1 comment:

  1. hi plz upload your ample also, I am not getting this.

    ReplyDelete