You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SharePoint 2016 右键文件自定义动作开发需求问询

Hey there! Let's work through this SharePoint custom right-click action challenge together. You already tried event receivers (wrong trigger timing) and SharePoint Designer (stuck on the follow-through), so let's get this working properly.

Solution Overview

We'll build a custom right-click action that triggers a dedicated logic layer (server-side code for On-Prem, Power Automate for Online) to handle the file copy and renaming. This avoids the event receiver timing issue and fixes the gaps in your SharePoint Designer setup.

1. Correct the SharePoint Designer Custom Action Setup

You started with SPD, so let's refine that first:

  • Open SharePoint Designer, connect to your site, and navigate to the target document library.
  • Go to Custom Actions → New Custom Action → List Item Menu (this ensures it shows up in the file right-click menu).
  • Fill in the basics:
    • Name: Copy with Username Suffix
    • Description: Copies the file to the same folder and appends your login name to the filename
    • Url: This is the critical piece—we'll fill this in after building the logic layer.

2. Build the File Copy & Rename Logic

Option A: SharePoint On-Premises (Server-Side Code)

If you're using SharePoint Server, create a custom application page to handle the heavy lifting:

  1. In Visual Studio, create a SharePoint Project and add an Application Page.
  2. In the code-behind, add this logic (adjust namespaces as needed):
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int itemId = int.Parse(Request.QueryString["ID"]);
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList docLib = web.Lists["YourDocumentLibraryName"];
                    SPListItem sourceItem = docLib.GetItemById(itemId);
                    SPFile sourceFile = sourceItem.File;
    
                    // Clean up login name (remove domain, invalid chars)
                    string loginName = web.CurrentUser.LoginName.Split('\\')[1];
                    loginName = loginName.Replace("@", "_").Replace(".", "_");
    
                    // Generate new filename
                    string originalName = Path.GetFileNameWithoutExtension(sourceFile.Name);
                    string extension = Path.GetExtension(sourceFile.Name);
                    string newFileName = $"{originalName}_{loginName}{extension}";
    
                    // Copy to same folder
                    string destinationUrl = sourceFile.ParentFolder.Url + "/" + newFileName;
                    sourceFile.CopyTo(destinationUrl, false); // false = no overwrite
                }
            }
            // Redirect back to the folder
            Response.Redirect(Request.QueryString["FolderUrl"]);
        }
    }
    
  3. Deploy the solution to your SharePoint farm.
  4. Update the SPD custom action's Url to point to your application page, passing required parameters:
    ~/_layouts/15/YourSolutionName/CopyFileWithUsername.aspx?ID={ItemId}&FolderUrl={FolderUrl}

Option B: SharePoint Online (Power Automate Flow)

For SharePoint Online, skip server-side code and use Power Automate:

  1. Create an Instant cloud flow triggered by Manually trigger a flow. Add a text input parameter named FileID.
  2. Add a SharePoint - Get file properties action:
    • Site address: Your SharePoint site URL
    • Library name: Target document library
    • Item ID: Use the FileID input from the trigger.
  3. Add a Compose action to build the new filename:
    concat(
        split(outputs('Get_file_properties')?['body/{FilenameWithExtension}'], '.')[0],
        '_',
        replace(triggerBody()?['UserEmail'], '@yourdomain.com', ''), // Replace with your domain
        '.',
        split(outputs('Get_file_properties')?['body/{FilenameWithExtension}'], '.')[1]
    )
    
  4. Add a SharePoint - Copy file action:
    • File to copy: Identifier from the Get file properties action
    • Destination folder: Folder path from Get file properties
    • New file name: Output from the Compose action
  5. Save the flow, then copy its trigger URL (found in the trigger settings).
  6. Update the SPD custom action's Url to this trigger URL, appending the file ID:
    https://prod-xx.westus.logic.azure.com:443/workflows/...?FileID={ItemId}

3. Test & Troubleshoot

  • Right-click a file in your library and select the custom action. Verify the copied file appears with the username suffix.
  • Common fixes:
    • Action not showing up: Double-check you selected "List Item Menu" in SPD and targeted the correct library.
    • Copy fails: Ensure the action/flow has edit permissions on the library.
    • Invalid filename: Clean the login name to remove characters like /, \, or : that SharePoint blocks.

内容的提问来源于stack exchange,提问作者John Seyfried

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:54:23