0

Current Environment: SharePoint online 2013

Could someone suggest on how to close the SharePoint Add-In modal dialog.

I have created a custom action on a library using a wcf service. When clicked on the custom action link with in the library I am displaying a modal dialog which has a custom hosted add-in page.

Upon some user selection & save button click I would like to close this modal dialog & open up a new word document based on a template.

The problem is I get access denied & assuming that the call back function is in a different page & hence access denied??

Here is the full code below. If I move the CoreInvoke statement which opens the doc, in to the client js "onFileCreated" function, I am able to open the document as desired, how ever I am not able to close the modal dialog as I was able to do in SP2010 application, because access being denied.

Here is the complete code:

Step 1: CSOM - Adding Custom action to the library using WCF service

UserCustomAction action = libraryList.UserCustomActions.Add(); action.Title = ActionTitle; action.Location = "CommandUI.Ribbon"; action.Sequence = 403; action.CommandUIExtension = GetCommandXml(); action.Update(); context.ExecuteQuery();




private string GetCommandXml()
        {
            string dialogUrl = "https://mysharepoint.com/TemplateRibbonActionAddIn/Pages/Templates.aspx";
            dialogUrl += "?SPListId=" + _currentListId
                         + "&TemplateSiteUrl=" + _templateLibrarySiteColUrl
                         + "&SPHostUrl=" + _hostWebUrl
                         + "&TemplateWebUrl=" + _templateLibraryWebUrl
                         + "&TemplateLibraryTitle=" + Microsoft.SharePoint.Client.Utilities.HttpUtility.UrlPathEncode(_templateLibraryTitle, true);
        string commandAction =
            "javascript: " +
            "function templateCallback(result, returnValue) { if (result == SP.UI.DialogResult.OK) { CoreInvoke('createNewDocumentWithProgIDEx', event, returnValue.templateUrl, returnValue.saveUrl, 'SharePoint.OpenDocuments', false); } } " +
            "var dialogUrl = '" + dialogUrl +"' ; " +
            "var options = {url: dialogUrl, title: 'Select Template', dialogReturnValueCallback: templateCallback, height: 800, width: 600}; " +
            "SP.UI.ModalDialog.showModalDialog(options);";

        string xml =
        "<CommandUIExtension>" +
            "<CommandUIDefinitions>" +
                "<CommandUIDefinition Location=\"Ribbon.Documents.New.Controls._children\">" +
                    "<Button Id=\"" + ActionButtonId + "\" " +
                        "Alt=\"New From Template\" " +
                        "Sequence=\"40\" " +
                        "Command=\"{C8119B79-DC95-4587-94F6-3767D549AF49}\" " +
                        "Image32by32=\"/_layouts/15/images/lg_icdotx.png\" " +
                        "Image16by16=\"/_layouts/15/images/icdotx.gif\" " +
                        "LabelText=\"Select Template\" " +
                        "TemplateAlias=\"o1\" />" +
                "</CommandUIDefinition>" +
            "</CommandUIDefinitions>" +

            "<CommandUIHandlers>" +
                "<CommandUIHandler " +
                "Command=\"{C8119B79-DC95-4587-94F6-3767D549AF49}\" " +
                "CommandAction=\"" + commandAction  + "\" />" +
            "</CommandUIHandlers>" +
        "</CommandUIExtension>";
        return xml;
    }       

With in the Hosted add in page, below function triggers on save button click

 ExecuteOrDelayUntilScriptLoaded(loadTemplates, "sp.js");
        $('.btnSave').click(function () {
            try {


            var selectedContentType = $('.ddlContentTypes').val();
            var selectedTemplate = $('.txtSelectedTemplate').val();
            if (selectedContentType == null || selectedContentType == '' || selectedTemplate == null || selectedTemplate == '') {
                 alert("Please make sure to select a template & content type before you click save.")
            } else {
                intialise();
                context = SP.ClientContext.get_current();

                var svcUrl = 

    "http://amigosito365.azurewebsites.net/MyHybridService.svc/CreateDocumentWithTemplateOptions/?SPHostWebUrl="
    + encodeURIComponent(hostWebUrl) + "&SPListId=" + spListId + "&TemplateSiteUrl=" + encodeURIComponent(templateSiteUrl) + "&LibraryTitle=" + encodeURIComponent(templateLibraryTitle) +"&" + selectedTemplate + "&ContentType=" + encodeURIComponent(selectedContentType);
                    var request = new SP.WebRequestInfo();

                    request.set_url(

                         svcUrl

                         );

                    request.set_method("GET");

                    request.set_headers({ "Accept": "application/json; odata=verbose" });


                    response = SP.WebProxy.invoke(context, request);

                    context.executeQueryAsync(onFileCreated, onQueryFailed);

                }



            } 


***********************************************************************

function onFileCreated(data,status,jqXHR) {
    var jsonData = data.$1L_0[1].$G_1;
    var data = JSON.parse(jsonData);
    if (data.Success) {
        if (data.TemplateFullUrl != "" && data.FileSaveUrl != "") {

            try {

                    ExecuteOrDelayUntilScriptLoaded(function () {

                    var args = {
                        templateUrl: data.TemplateFullUrl,
                        saveUrl: data.FileSaveUrl

                    };


                }, "SP.UI.Dialog.js");
            } catch (e) {
                alert(e.message + " Stack: " + e.stack);

            }

        }

    } else {
        alert("Error occurred while creating the template file.")
    } }

Any help would be highly appreciated.

Thanks in Advance.

1 Answer 1

0

I have managed fix the above issue as suggested in the post by Alex Terentiev:

Here is my updated fileCreated function, I had to comment the ExecuteOrDelayUntilScriptLoaded(function () ) and call the coreinvoke to open the document as required and post the message to parent to close dialog.

 if (data.TemplateFullUrl != "" && data.FileSaveUrl != "") {

        try {

                //ExecuteOrDelayUntilScriptLoaded(function () {

               // var args = {
                //    templateUrl: data.TemplateFullUrl,
                //    saveUrl: data.FileSaveUrl

               // };


            //}, "SP.UI.Dialog.js");
CoreInvoke('createNewDocumentWithProgIDEx', event, data.TemplateFullUrl, data.FileSaveUrl, 'SharePoint.OpenDocuments', false);

 // posting a message
 var target = parent.postMessage ? parent : 
(parent.document.postMessage ? parent.document : undefined); 
 if (target) 
target.postMessage('CloseDialog', '*');

        } catch (e) {
            alert(e.message + " Stack: " + e.stack);

        }

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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