【问题标题】:SharePoint 2016 Custom Action to trigger a workflow用于触发工作流的 SharePoint 2016 自定义操作
【发布时间】:2018-10-07 13:39:51
【问题描述】:

我正在尝试创建一个自定义操作,可以通过右键单击特定 SharePoint 库的任何文件夹中的文件来选择该操作。此自定义操作会将文件复制到同一文件夹中,并将用户的登录名附加到文件名的末尾。

我目前有一个事件接收器,它将在文件更新时执行自定义操作,但我不希望它发生。我能够使用 SharePoint Designer 将自定义操作添加到右键单击文件菜单,但 SharePoint Designer 仅允许自定义操作触发特殊的 SharePoint 2010 兼容工作流或加载网页。我需要这样做,以便当用户在右键单击文件后选择自定义操作时触发事件处理程序(或可能是工作流)。我不确定我需要在 Visual Studio 2017 中创建什么方法或什么样的项目或应用程序才能获得此功能。

【问题讨论】:

    标签: sharepoint visual-studio-2017 custom-action sharepoint-2016


    【解决方案1】:

    您的自定义操作应调用javascript function 或执行GET request 到您的SharePoint 托管WCFASMX WebService。

    • ASMX

    Official MSDN Walktrought: Creating a Custom ASP.NET Web Service

    更多截图资源请查看this blog post: Walkthrough: Creating a Custom ASP.NET (ASMX) Web Service in SharePoint 2010

    • WCF

    Official Technet Walkthrough: SharePoint 2013: Create a Custom WCF REST Service Hosted in SharePoint and Deployed in a WSP

    注意:使用GET request,您将需要web.AllowUnsafeUpdate = true

    使用javascript你需要AJAX调用即jQuery.ajax()

    /编辑

    要使用 SharePoint Desinger 连接 Web 服务和您的自定义操作,删除或更改您现有的自定义操作,将类型更改为 Navigate to URL 并在文本框中输入:

    javascript: (function() { console.log('Testing...' + {ItemId}); /* your web service call */ })();
    

    使用 {ItemId} 别名将正确的项目 ID 传递给您的 AJAX 调用。

    另一方面,在 Web 服务端使用 SPWorkflowManager 类来启动项目的工作流。检查下面的代码(link):

    public void StartWorkflow(SPListItem listItem, SPSite spSite, string wfName)  {
        SPList parentList = listItem.ParentList;
        SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations;
        foreach (SPWorkflowAssociation association in associationCollection)  {
          if (association.Name == wfName){
           association.AutoStartChange = true;
           association.AutoStartCreate = false;
           association.AssociationData = string.Empty;
           spSite.WorkflowManager.StartWorkflow(listItem, association,  association.AssociationData);
           }
         }
    }
    

    【讨论】:

    • 感谢您的快速响应!我将查看这些链接,看看我是否可以创建一个 Web 服务来解决问题。但是,我觉得我目前的障碍是我找不到将自定义操作连接到工作流或触发事件或 Web 服务调用的示例。假设我完成了一个新的 Web 服务来解决问题。我仍然需要将自定义操作连接到 SharePoint 库,并且我需要让它调用 Web 服务或工作流。我觉得我可能没有在正确的地方寻找或没有使用正确的搜索词来解决这个问题。
    【解决方案2】:

    我找到了一种使用 JavaScript 的方法,无需 SharePoint Designer。我将以下脚本放在列表视图 Web 部件所在页面上的内容编辑器 Web 部件中,现在我可以右键单击文件并获得“获取我的副本”选项。如果您有一个 Comments 子文件夹,重命名的副本将放在那里。

    <script type="text/javascript">
    
    // adds the menu option to Get My Copy
    function Custom_AddDocLibMenuItems(m, ctx)
    {
        var strDisplayText = "Get My Copy";                  //Menu Item Text
        var strAction = "copyFile()"; 
        var strImagePath = "";                               //Menu item Image path
        CAMOpt(m, strDisplayText, strAction, strImagePath);  // Add our new menu item
        CAMSep(m);                                           // add a separator to the menu
        return false;                                        // false means standard menu items should also be rendered
    }
    
    // append current user account to filename and copy to subfolder named Comments 
    function copyFile()
    {
        // get web and current user from context
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        this.currentUser = web.get_currentUser();
        context.load(currentUser);
    
        // load the folder
        var currentFolder = decodeURIComponent(ctx.rootFolder);
        var folderSrc = web.getFolderByServerRelativeUrl(currentFolder); 
        context.load(folderSrc,'Files');
    
        context.executeQueryAsync(
            function() {            
                // get the first (and hopefully only) file in the folder
                var files = folderSrc.get_files();
                var e = files.getEnumerator();
                e.moveNext()    
                var file = e.get_current();
    
                // get user account
                var curUserAcct = currentUser.get_loginName();
                curUserAcct = curUserAcct.substring(curUserAcct.indexOf("\\") + 1); 
    
                // get file without extension
                var file_with_ext = file.get_name();            
                var name_without_ext = file_with_ext.substr(0, file_with_ext.lastIndexOf("."));
    
                var destLibUrl = currentFolder + "/Comments/" + name_without_ext + " " + curUserAcct + ".docx";     
                file.copyTo(destLibUrl, true);
    
                context.executeQueryAsync(
                    function() { alert("Success! File File successfully copied to: " + destLibUrl); }, 
                    function(sender, args) { alert("error: " + args.get_message()) }
                );
            }, 
            function(sender, args){ alert("Something went wrong with getting current user or getting current folder '" + currentFolder + "'. " + args.get_message()); }
        );
    }
    
    </script>
    

    【讨论】:

    • 更新:我找到了一种方法可以让 FILES 和 LIBRARY 选项卡表单消失。如果您使用 SharePoint Designer,您可以在高级模式下编辑“所有文档”默认视图(或者更好的是,创建您自己的视图并对其进行编辑)并将上述 javascript 放入 .aspx 页面的 placeholderMain 块中。
    • 我的方法不需要共享点设计器,我只是假设这是您创建菜单项的方式。 SPD 在后台创建自定义操作,并且可以使用 SOM、CSOM、JSOM 以及可能使用 REST 创建它们。您现在正在做的是使用可能在 sharepoint 2003 中使用的 sharepoint 全局函数...
    猜你喜欢
    • 1970-01-01
    • 2011-04-30
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多