Quantcast
Channel: Techaholica
Viewing all articles
Browse latest Browse all 10

SharePoint: Version a Document Set through a Workflow

$
0
0

The Problem

So I have been playing around with Document Sets lately and I really like them. They are an awesome tool, but I just found an unusual bug (feature?). It turns out you cannot version Document Sets automatically. If you go into your library settings and turn on versioning, that only applies to the documents within the Document Sets. To version your Document Set, you have to click on the Document Set, click Manage from the ribbon, then click Capture Version.

CaptureVersion

Well this works fine, but it is a manual process and you are trusting to users to do this every time they update the properties of a Document Set. If you are like me, then you thought about creating a workflow which runs every time the Document Set is updated and versions it. This workflow may look like this.

DocumentSetWorkflow

Notice I added a line to check that we are working with a Document Set so that this doesn’t try to run on other Content Types. Well you may think you are done, but you are in fact just getting started. Go make an update to a Document Set, then take a look at the version history. You will probably see something like this.

DocumentSetBug

Notice how it captures the version correctly once, then does ten more versions for no reason at all? For some reason the workflow will version the Document Set ten times when you call this action. I searched high and low and saw that others were running into this issue as well, but nobody gave a good fix. I decided to get a little creative and this is what I came up with.

The Solution

First create a new field called AutoVersion (or whatever you like). Make this a Yes/No checkbox.

AutoVersionField

Go back to your workflow and hit refresh so that your new field shows up in designer. Add a new line which checks that the AutoVersion box is checked before running. After running, uncheck this box.

DocumentSetWorkflowUpdated

What will happen, is the workflow will see the the box is checked and version the Document Set and immediately uncheck the field. Now the 2-10 other times the workflow runs, it will see the box is unchecked and will not perform the versioning action.

Publish your changes and go back to your list and edit the properties of a Document Set. Be sure you check the AutoVersion box in the edit properties window.

DocumentSetPropertiesUpdated

After you save your changes, take a look at the Document Set Version History and you should see just one new version for your document set.

DocumentSetVersions

Now at this point you are basically done if you are fine with leaving it up to your users to check that box to make sure the document set is versioned. If that was the case, you probably wouldn’t need this workflow at all because you would just have your users click the Capture Version button in the ribbon.

The second part to my solution is some jQuery added to the Edit Properties window. Here’s how you do it.

First go to List Settings > Advanced Settings. Under the Dialogs section, click No where it asks “Launch forms in a dialog”. This will make it so that when you click Edit Properties, it opens in a full page rather than the pop up windows. We want that so that we can edit the page easily.

Now click Edit Properties on any of your document sets then click Site Actions > Edit Page. This puts the page in a modifiable state so we can make changes to it.

DocumentSetEditProperties

Click Add Web Part and add a Content Editor web part from the Media and Content category. Drag the new web part below your content type properties section so that it is below everything else on the page.
Modify the properties of the web part so that it is hidden. The checkbox is under Layout.
Now click where it says “Click here to add new content”. From the ribbon choose, HTML > Edit HTML Source.
Copy and paste the below code and click Ok. I will explain what is going on here a little later, but basically we are checking the box for the user then hiding the checkbox so the user doesn’t even know that there was a checkbox there. To SharePoint though, they checked the box and the workflow picks up on that.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){

    // below you have three options for how you want to handle the AutoVersion checkbox
    // Choose which method you want to use and comment out or delete the other lines
     
    // 1. Uncomment the below line if you want to check the checkbox but leave it enabled
    // $('input[title="AutoVersion"]').attr('checked', true);
   
    // 2. Uncomment the below line to disable the checkbox as well as checking the box
    //$('input[title="AutoVersion"]').attr('checked', true).attr('disabled','disabled');
   
    // 3. Uncomment the below line to check the box and then hide it from the user altogether
    $('input[title="AutoVersion"]').attr('checked', true).closest('tr').hide();
   
});

</script>

Click Stop Editing from the ribbon to save the changes you made to the page. Go back to library settings and turn on the dialogs again and you are done! Go edit the properties of a document set, and you should notice that there is not an AutoVersion checkbox anymore.

DocumentSetEditPropertiesUpdated

Save your changes and you will see that it still got versioned correctly.

DocumentSetVersionsWorking

So now back to the code. You have three options to choose from.
1. Check the AutoVersion checkbox automatically, but leave it enabled. This gives users the chance to uncheck it if they don’t want to take a version of the document set.
2. Check the AutoVersion checkbox automatically, but disable the checkbox so they can’t uncheck it. This is if you want to make it clear to the user that the document set will be versioned when they save their changes.
3. Check the AutoVersion checkbox automatically, then hide it. This is the default option and is probably the best option since to the user, the document set is getting versioned off just like any other document.

To switch between the options, uncomment out one of the lines (remove the // in front of the code), and comment off the others (add the // in front of the code).
Hope this helps!


Viewing all articles
Browse latest Browse all 10

Trending Articles