Un esempio per SharePoint 2010 su come associare, via codice (C#), una custom action ad uno specifico content type.

In questo caso la custom action è sul menu contestuale dell'item (EditControlBlock):

C#

string url = "http://sharepoint.sgart.int";
using (SPSite site = new SPSite(url))
{
  using (SPWeb web = site.OpenWeb())
  {
    SPWeb webRoot = site.RootWeb;
    //recupero il content type a cui voglio associare la custom action
    SPContentType ct = webRoot.ContentTypes["MSurvey"];

    web.UserCustomActions.Clear();
    SPUserCustomAction action = web.UserCustomActions.Add();

    action.RegistrationType = SPUserCustomActionRegistrationType.ContentType;
    action.RegistrationId = ct.Id.ToString();

    action.Location = "EditControlBlock"; // menu contestuale
    action.Sequence = 450;
    action.Title = "Edit Questions";

    action.Rights = SPBasePermissions.EditListItems; //visibile solo dagli utenti che hanno il permesso di editing

    action.ImageUrl = "/_Layouts/images/MIP/survey/question16.png";

    action.Url = "{SiteUrl}/_Layouts/Survey/EditQuestion.aspx?SurveyID={ItemId}&Source={Source}";
    //action.Url = "javascript:OpenPopUpPageWithTitle('{SiteUrl}/_Layouts/Survey/EditQuestion.aspx?SurveyID={ItemID}&Source={Source}', RefreshOnDialogClose, 600, 400,'Survey Edit Questions')";
    action.Update();
  }
}

e questo è l'xml per fare la stessa cosa tramite feature:

XML

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <CustomAction Id="MEditQuestions"
    RegistrationType="ContentType"
    RegistrationId="0x010069EBA7A1B15944179CA76599D560E1E0"
    Location="EditControlBlock"
    Sequence="450"
    Title="Edit Questions"
    Description="Edit all questions of survey"
    Rights="EditListItems"
    ImageUrl="/_Layouts/images/survey/question16.png">
    <UrlAction Url="javascript:location.href='{SiteUrl}/_Layouts/Survey/EditQuestion.aspx?SurveyID={ItemId}&Source={Source}'" />
  </CustomAction>

</Elements>
Tags:
C#236 SharePoint498 SharePoint 2010224
Potrebbe interessarti anche: