1

I made new solution for custom actions run by on custom ribon.

Code from eventreciver:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
    string webRoot_URL = web.Url;
    SPSite site = new SPSite(webRoot_URL);

    using (SPWeb webRoot = site.OpenWeb())
    {
        SPList list = web.Lists.TryGetList(SYG_AcceptOnList_faktury.Properties.Resources.s_lista);
        if (list != null)
        {
            var actionUnlockPayment = list.UserCustomActions.Add();
            actionUnlockPayment.Location = "CommandUI.Ribbon.ListView";
            actionUnlockPayment.Rights = SPBasePermissions.ManageLists
                | SPBasePermissions.CancelCheckout | SPBasePermissions.AddListItems
                | SPBasePermissions.EditListItems | SPBasePermissions.DeleteListItems
                | SPBasePermissions.ViewListItems | SPBasePermissions.ApproveItems
                | SPBasePermissions.OpenItems | SPBasePermissions.ViewVersions
                | SPBasePermissions.DeleteVersions | SPBasePermissions.CreateAlerts
                | SPBasePermissions.ViewFormPages | SPBasePermissions.ManagePermissions
                | SPBasePermissions.ViewUsageData | SPBasePermissions.ManageWeb
                | SPBasePermissions.AddAndCustomizePages | SPBasePermissions.ApplyThemeAndBorder
                | SPBasePermissions.ApplyStyleSheets | SPBasePermissions.BrowseDirectories
                | SPBasePermissions.CreateSSCSite | SPBasePermissions.ViewPages
                | SPBasePermissions.EnumeratePermissions | SPBasePermissions.BrowseUserInfo
                | SPBasePermissions.ManageAlerts | SPBasePermissions.UseRemoteAPIs
                | SPBasePermissions.UseClientIntegration | SPBasePermissions.Open
                | SPBasePermissions.EditMyUserInfo | SPBasePermissions.CreateGroups
                | SPBasePermissions.ManagePersonalViews | SPBasePermissions.AddDelPrivateWebParts
                | SPBasePermissions.UpdatePersonalWebParts;
            actionUnlockPayment.Sequence = 21;
            actionUnlockPayment.Title = "Akceptuj dokumenty";
            actionUnlockPayment.CommandUIExtension = @"<CommandUIExtension><CommandUIDefinitions>
            <CommandUIDefinition Location=""Ribbon.ListItem.Manage.Controls._children"">
            <Button Id=""{245CAE99-A250-4E2F-8448-A108ABE688A3}"" Sequence=""100"" TemplateAlias=""o1""
             Image16by16=""_layouts/15/SYG_AcceptOnList_faktury/Images/ikonka_akceptuj_16px.png""
             Image32by32=""_layouts/15/SYG_AcceptOnList_faktury/Images/ikonka_akceptuj_32px.png""
            Command=""{A139C8C3-3475-4DAE-92AB-57C49C542D6C}"" CommandType=""General"" LabelText=""Akceptuj dokumenty"" />
            </CommandUIDefinition>
            </CommandUIDefinitions>
            <CommandUIHandlers>
            <CommandUIHandler Command =""{A139C8C3-3475-4DAE-92AB-57C49C542D6C}"" 
             EnabledScript=""javascript:function isEnabled(){var selectedCourses = SP.ListOperation.Selection.getSelectedItems();return(selectedCourses.length >= 1);} isEnabled();""                    
             CommandAction=""javascript:AccceptFunctions();"" />
            </CommandUIHandlers></CommandUIExtension>";
            actionUnlockPayment.Update();

            var scriptlink = site.UserCustomActions.Add();
            scriptlink.Location = "ScriptLink";
            scriptlink.ScriptSrc = "/_layouts/15/SYG_AcceptOnList_faktury/js.js";
            scriptlink.Update();
        }
    }
}

In js.js:

function AccceptFunctions() {
    var ctx = SP.ClientContext.get_current();
    var clistUrl = ctx.$w_0;
    var ItemIds = "";
    var listId = SP.ListOperation.Selection.getSelectedList();
    var web = ctx.get_web();
    var list = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList())
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
    for (var i = 0; i < selectedItems.length; i++) {
        ItemIds += selectedItems[i].id + ",";
    }

    var pageUrl = SP.Utilities.Utility.getLayoutsPageUrl('/SYG_AcceptOnList_faktury/AccceptPage.aspx?ids=' + ItemIds + '&listid=' + listId + '&listurl=' + clistUrl);
    var options = SP.UI.$create_DialogOptions();
    options.width = 450;
    options.height = 150;
    options.url = pageUrl;
    options.dialogReturnValueCallback = Function.createDelegate(null, OnDialogClose);
    SP.UI.ModalDialog.showModalDialog(options);
}

And finally in AcceptPage:

protected void Button_OK_Click(object sender, EventArgs e)
{
    try
    {
        Button_OK.Enabled = false;
        Button_Canel.Enabled = false;
        LogIMG.Visible = true;
        LogText.Visible = true;
        using (SPSite oSite = new SPSite(SPContext.Current.Site.ID))
        {
            using (SPWeb oWeb = oSite.OpenWeb())
            {
                oWeb.AllowUnsafeUpdates = true;
                SPList oList = oWeb.Lists.TryGetList(Properties.Resources.s_lista);
                if (ItemIDs.Count > 0)
                {
                     foreach (int item in ItemIDs)
                     {
                         SPListItem ListItem = oList.GetItemById(item);
                         if (ListItem != null)
                         {
                             tytuly += ListItem.Title + ", ";
                             ListItem[Properties.Resources.s_pole] = Properties.Resources.s_wartosc.ToString();
                             ListItem.Update();
                         }
                     }
                }
                oWeb.AllowUnsafeUpdates = false;
            }
        }
    }
    catch (Exception eA)
    {
        LogIMG.ImageUrl = "Images/icons8-Bug-48.png";
        LogText.ForeColor = System.Drawing.ColorTranslator.FromHtml("#d82929");
        LogText.Text = eA.ToString();
    }
    tytuly = tytuly.Substring(0, (tytuly.Length - 2));
    LogIMG.ImageUrl = "Images/icons8-Approval-48.png";
    LogText.ForeColor = System.Drawing.ColorTranslator.FromHtml("#006600");
    LogText.Text = "Zaakceptowano: " + tytuly + ".";
    Button_Canel.Text = "Zamknij";
    Button_Canel.Enabled = true;
}

Ok, my problem. Its work greate on root web. But dont work on subsite, that code look for list on root web. I dont know what edit or use to force code to work on sub site. Maybe the ModalDialog is run on root? Maybe i need sand name/url my list on subsite by javascript to dialog?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.