Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am facing trouble removing the "Edit Item" ribbon button from calendar DispForm.aspx.

I have created a visual studio 2010 user control where i used the method TrimById("Ribbon.ListForm.Display.Manage.EditItem"); and deployed the solution.

Next I went into calendar's master page and used the control created in the Register tag. If it worked the "Edit Item" in DispForm.aspx should not appear, but it does. Please tell me if I did miss any steps.

share|improve this question
add comment

1 Answer

SharePoint provides ability to remove ribbon elements using custom actions. This method was tested and I think it is more preferrable and much more flexible, than inserting your custom controls into your masterpage. At least, because custom actions approach is available for sandboxed solutions.

In your case, you probably should use following code to define such custom action:

  <CustomAction
   Id="RemoveEditItemButtonFromCalendar"
   Location="CommandUI.Ribbon.DisplayForm"
   RegistrationType="List"
   RegistrationId="106"
    >
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.ListForm.Display.Manage.EditItem" />
      </CommandUIDefinitions>
    </CommandUIExtension>
  </CustomAction>

106 here is the ID of Events list template. If you have your custom list template or standard template other than Events, you should use it instead of 106 value.

If you want to attach your ribbon customizations to a single list, rather than to a list template, you can use SPList.UserCustomActions collection. In your feature receiver you should place following code:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    // considering your feature is web-scoped
    var web = properties.Feature.Parent as SPWeb;

    // fetch SPList object, it must have been created before using this code
    var list = web.GetListFromUrl("path/to/list/AllItems.aspx");

    // create new customAction
    var customAction = list.UserCustomActions.Add();

    // this is for cleanup purposes in FeatureDeactivating
    customAction.Name = properties.Feature.DefinitionId.ToString("N") + "_ribbon";

    // setting custom action location, and the CommandUIExtension element
    customAction.Location = "CommandUI.Ribbon.DisplayForm";
    customAction.CommandUIExtension = @"
      <CommandUIExtension>
        <CommandUIDefinitions>
          <CommandUIDefinition Location=""Ribbon.ListForm.Display.Manage.EditItem"" />
        </CommandUIDefinitions>
      </CommandUIExtension>"

    // saving the changes to database
    customAction.Update();
    list.Update();
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    // cleanup code

    SPWeb web = properties.Feature.Parent as SPWeb;
    var list = web.GetListFromUrl("path/to/list/AllItems.aspx");

    bool collectionChanged = false;
    for (int i = list.UserCustomActions.Count - 1; i >= 0; i--)
    {
        var customAction = list.UserCustomActions.ElementAt(i);

        if (customAction.Name.StartsWith(properties.Feature.DefinitionId.ToString("N")))
        {
            customAction.Delete();
            collectionChanged = true;
        }
    }

    if (collectionChanged)
        list.Update();
}
share|improve this answer
    
Hello omlin, the custom action you mentioned must be used in Elements.xml file if I am not mistaken. But I had a client requirement where I need to remove some other ribbon buttons as well. So the best thing to do was to use Visual studio and utilize the code in usercontrol. Please do suggest some solutions so that I can make my code work. –  Chandra Shekhar May 23 '11 at 11:53
    
Yes, it's done with Elements.xml. But you can define as many CommandUIDefinition elements, as you wish, so you can remove several buttons at one time. I think using elements.xml here is much better, because it will work for sandboxed solutions too, unlike the usercontrol solution. And also this method was tested many many times and it works perfectly... –  Andrey Markeev May 23 '11 at 12:01
    
Hello omlin, thanks for the quick response. Could you please tell me how to do this in visual studio and how to actually remove buttons using the Elements.xml file. Please provide some references on how to achieve it. Thanks –  Chandra Shekhar May 23 '11 at 12:04
    
Hello, I need the information about the Id and RegistrationId. I have the customized list so obviously the RegistrationId won't be 106. So how do I actually find/set the RegistrationId for my custom list. And is this Id="RemoveEditItemButtonFromCalendar" customizable or is the default Id to be used when removing the buttons??? And also by using Elements.xml the "Edit Button" will be removed for all the calendar's present in the site. How to restrict it to just one calendar only. Thanks –  Chandra Shekhar May 23 '11 at 12:24
    
Id can be whatever you want. Registration Id must point to your list template id. This will assign your customization to all the lists with specified template (including even non-existant). For assigning customizations to concrete existing list, you can use SPList.UserCustomActions. If you give me a few hours, I'll update my answer with detailed explanation how all this can be done (now I have no chance to do this because of lack of time at work). –  Andrey Markeev May 23 '11 at 14:15
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.