Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have found that using a Visual Studio workflow allows me the most flexibility to do this. A SharePoint Designer Workflow would be simpler, but would be harder to deploy to different sites and libraries.</p> <p>After reading some good articles including <a href="http://ikarstein.wordpress.com/2011/03/30/walkthrough-creating-a-simple-sequential-workflow-with-a-custom-task-form-in-sharepoint-2010-using-visual-studio-2010-part-2-of-2/" rel="nofollow noreferrer">this</a> and <a href="http://msdn.microsoft.com/en-us/library/gg265727.aspx" rel="nofollow noreferrer">this</a> I have come up with this code which seems to work. It starts a workflow and waits until the document is not in a LockState and then processes the filename.</p> <p>The workflow looks like this:</p> <p><img src="https://i.stack.imgur.com/O3o1h.png" alt="Workflow image"></p> <p>And here is the code behind:</p> <pre><code>namespace ControlledDocuments.RenameWorkflow { public sealed partial class RenameWorkflow : SequentialWorkflowActivity { public RenameWorkflow() { InitializeComponent(); } public Guid workflowId = default(System.Guid); public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties(); Boolean continueWaiting = true; private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e) { CheckFileStatus(); } private void whileActivity(object sender, ConditionalEventArgs e) { e.Result = continueWaiting; } private void onWorkflowItemChanged(object sender, ExternalDataEventArgs e) { CheckFileStatus(); } private void CheckFileStatus() { if (workflowProperties.Item.File.LockType == SPFile.SPLockType.None) { continueWaiting = false; } } private void renameFile(object sender, EventArgs e) { try { SPListItem currentItem = workflowProperties.Item; if (currentItem["_dlc_DocId"] != null) { string docId = currentItem["_dlc_DocId"].ToString(); if (!currentItem["BaseName"].ToString().StartsWith(docId)) { currentItem["BaseName"] = docId + currentItem["BaseName"]; currentItem.SystemUpdate(); } } } catch (Exception ex) { //Should do something useful here } } } } </code></pre> <p>Hope this helps someone else if they have the same problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload