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.
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
- Create a new Empty SharePoint 2013 project in Visual Studio. I selected to create this as a sandbox solution.
- 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.
- Right click on your project and select Add > New Item…
Select Module and call it ‘Assets’
- Delete the sample.txt and copy jQuery over into the empty module.
Your module should now look similar to this:
- 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.
- 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.
1234567<?xml version="1.0" encoding="utf-8"?><Elements xmlns="http://schemas.microsoft.com/sharepoint/"><Module Name="Assets" Path="Assets" Url="SiteAssets/HideLibrarySelect"><File Url="HideLibrarySelection.js" Type="GhostableInLibrary" /><File Url="jquery-1.9.1.min.js" Type="GhostableInLibrary" /></Module></Elements>
Right click on the project and select Add > New Item…, from the list select Empty Element and give it the name ‘CustomActions’.
- Open Elements.xml, if it hasn’t opened already and paste in the following XML – update any file references as required:
1234567891011<?xml version="1.0" encoding="utf-8"?><Elements xmlns="http://schemas.microsoft.com/sharepoint/"><CustomActionScriptSrc="~Site/SiteAssets/HideLibrarySelect/jquery-1.9.1.min.js"Location="ScriptLink"Sequence="85" /><CustomActionScriptSrc="~Site/SiteAssets/HideLibrarySelect/HideLibrarySelection.js"Location="ScriptLink"Sequence="90" /></Elements>
- Ensure that the Module and Element are included with our Feature. Deploy the solution.
- 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.