SharePoint: Hiding Destination Library option when uploading assets

SharePoint 2013 provides users with the ability to upload photos directly from pages that they are editing. This is a nice piece of functionality that avoids the user from first having to upload these photos manually.

There is an option for each library under Advanced Settings where it can be defined as a Site Assets library. This option simply selects it as the default location when users upload images or other resources on the site.  

1

But what if you want to have finer control over this? From what I’ve found, there is no way to confine images to reside in one library while other file types default to another library. Sometimes this additional choice just confuses users and then you end up with images spread out unnecessarily across multiple libraries.

In the steps below I will run through a brief example of creating a sandbox solution that works around this issue using jQuery.

 

Creating the solution

  1. Create a new Empty SharePoint 2013 project in Visual Studio. I selected to create this as a sandbox solution.
  2. Right click Features and select Add Feature. Rename this feature to something a bit more meaningful and set its scope to be at the Web Level.

     6

  3. Right click on your project and select Add > New Item…

    Select Module and call it ‘Assets’

     2 

  4. Delete the sample.txt and copy jQuery over into the empty module.
  5. Create a new JavaScript file, call it HideLibrarySelection.js. This will be used to store the jQuery code that will hide the drop down list box.

    Your module should now look similar to this:

     3

  6. Open HideLibrarySelection.js and paste in the following code. Update the libraryDefault variable to the display name of the library where you want the images to reside.

    This code checks the current page URL contains ‘_layouts/15/Upload.aspx’ and will only run when you are uploading an image (via Insert > Picture > From Computer).

  7. Open the Elements.xml and amend the module to be similar to the following (note your filenames may be slightly different). This will deploy our assets to the Site Assets library within the site. The files will be placed in a folder called HideLibrarySelect.
  8. To link this all up we must register the JavaScript on each page in the SPWeb where the feature has been activated. To do this we will use a Custom Action that adds our libraries to each page.

    Right click on the project and select Add > New Item…, from the list select Empty Element and give it the name ‘CustomActions’.

  9. Open Elements.xml, if it hasn’t opened already and paste in the following XML – update any file references as required:
  10. Ensure that the Module and Element are included with our Feature. Deploy the solution.
  11. Go to a wiki page and select Insert from the ribbon – Picture > From Computer. You should notice that Destination Library no longer appears on the form. Instead, the code automatically selects the library specified and then hides it from the form.

     4

    To test out the conditions in the JavaScript code, we can instead try to upload a file via a Wiki page. In this case the Destination Library should still be selectable.

     5

Leave a Reply

Next ArticleCard sorting adventures - Part 1