7/01/2019

Dynamic Images in SharePoint Document Sets

One of the cool features of Document Sets is that you can customize the "home page" of the Document Set by adding or editing its Content Type's home page web parts. The default page includes an image web part that can be customized with your choice of image. The only problem here is that the image is then used for all Document Sets created from this Content Type. Here's the default "home page" with the default image:

   

If you would like to change this image for each new Document Set added to a library, then upload a picture somewhere in SharePoint and then while viewing the new Document Set, click the Page ribbon, Edit Page and then edit the image web part. No fun if you have to do this for each new Document Set folder added to the library.
   

Make it Dynamic

What if you wanted the picture to be more automatic, or updateable by the users of the Document Set who might not have the permissions to edit the page?

Here's the plan:
  • Let the users of the Document Set upload a picture to the Document Set. We will pick a required named like "TeamLogo.png".
  • Modify the original Content Type in Site Settings, or modify the copy of that Content Type that is already associated with the library. (Content Types are copied to a library.)
    • Add a Content Editor Web Part just below the existing Image Web Part.
    • Add JavaScript to change the URL of the Image Web Part's HTML to point to our picture.

The steps below assume you have already created your custom Document Set Content Type.
  1. Go to Site Settings and Site Content Types and click the Content Type, or
    Go to the library, visit Library Setting and click the Content Type.
  2. Click Document Set Settings.
  3. Scroll down and click Customize the Welcome Page.
  4. In the ribbon click PAGE and Edit Page.
  5. Click below the Image web part (but still in Zone 1).
  6. Click the INSERT ribbon and click Web Part.
  7. In the Media and Content section click Content Editor and the click Add.
  8. If the web part did not go below the Image web part:
    1. In the Content Editor Web Part click the dropdown arrow and click Edit Web Part.
    2. Expand Layout, change the Zone Index from 0 to 1 and click Apply.
  9.  In the Content Editor Web Part click "Click here to add new content".
  10. Click the FORMAT TEXT ribbon and click Edit Source.
  11. Add the JavaScript from below.
  12. Replace "\\myimage.png" filename with something better like "\\teamlogo.pgn" or \\projectlogo.prg.
  13. Click OK and then APPLY. (no image will display while in the page editor)
  14. Click the PAGE ribbon and click Stop Editing.
  15. If you just created a new Content Type, go create a new library and add this Content Type to the library.
  16. In your library that is using this Content Type:
    1. Click the New button (above the list of files or in the ribbon) and create a new Content Type folder.
         
    2. Create a logo file and upload it to the Content Type folder. The image should be displayed in the Content Type home page area.

                        

The JavaScript code.


<script>
  // get the page's URL
  var url = window.location.href;
  // make sure we are not in page edit mode
  if(url.indexOf("RootFolder")>0)
  {
    // get the "RootFolder" query string parameter
    var rootpath = url.split("RootFolder=")[1].split("&")[0];
    
    // build a URL to our image file. Replace "myimage.png" with something
    // better like "\\teamlogo.pgn" or "\\projectlogo.prn"
    var imgpath = "..\\..\\.." + rootpath.replace(/%2F/g, "\\") + "\\myimage.png"; 
    // update the image source
    // Note that the ID is for the default doc set image web part. Yours
    // may be different!
    document.getElementById("MSOImageWebPart_WebPartWPQ2").src=imgpath;
  }
</script>
 ...

No comments:

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.