用于触发工作流的SharePoint 2016自定义操作

时间:2018-04-26 18:47:44

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

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

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

2 个答案:

答案 0 :(得分:0)

您的自定义操作应致电javascript function或对您的SharePoint托管GET requestWCF WebService执行ASMX

  • 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()

<强> /修改

要连接Web服务和自定义操作,请使用SharePoint Desinger,删除或更改现有的自定义操作,将类型更改为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);
       }
     }
}

答案 1 :(得分:0)

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

<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>