1

I have two metadata drop-down columns added on a Shared Documents library upload form. I am leveraging SPServices to perform cascading and its working nicely. These drop-downs are: Project and Company Name.

Project Drop-down contains choices that are subsite names like Test Site 1, Test Site 2, Test Site 3. Now if a user is working on Shared Documents Library of Test Site 1 then on the library edit form, i would like to add code that reads the site name (Test Site 1) and finds that value from Choice drop-down and set it to Test Site 1 so that way users don't have to filter Project drop-down each time they perform an upload.

Can someone please help me with the JSOM code since I am working with SharePoint online, thanks in advance.

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/JavaScript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
ExecuteOrDelayUntilScriptLoaded(retrieveWebSite, "sp.js");
});


function retrieveWebSite() {
   var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);
   this.oWebsite = clientContext.get_web();
   clientContext.load(this.oWebsite);
   clientContext.executeQueryAsync(
      Function.createDelegate(this, this.onQuerySucceeded), 
      Function.createDelegate(this, this.onQueryFailed)
    );
}

function onQuerySucceeded(sender, args) {
    var sitename = this.oWebsite.get_title();
    alert(sitename);
    $("select[title='Project Name']").val(sitename);
    sitename=null;
} 

function onQueryFailed(sender, args) {
  alert('Request failed. ' + args.get_message() + 
      '\n' + args.get_stackTrace());
} 
</script>
1

This js snippet

var context = new SP.ClientContext;
var url = context.get_url();

or this one

var url = _spPageContextInfo.webServerRelativeUrl

should return the sites down to the current subsite - and then I guess you can just use regex to narrow it down to the last sitename? Something like

var sitename = url.substr(url.lastIndexOf('/') + 1);

Which should give you "Test Site 1"

  • Thanks Anerd. This is one part though, how to now set the Project Drop-down to Test Site 1 if the read site name is Test Site 1? – mdevm Jul 18 '18 at 13:00
1

Setting the correct value in the dropdown you can do by using jQuery, example:

var url = _spPageContextInfo.webServerRelativeUrl
var sitename = url.substr(url.lastIndexOf('/') + 1);

$("select[title='Project']").val(sitename)

Obviously change the title of the dropdown to whatever the title of yours is, or use partial ID instead. Using ID is better, as this is based on the internal name of the column, and not the Display Name - and Display Name might change at some point and break your logic, while internal name will not change.

Edit : Edited the answer here, since comments are horrible for longer text

Are you absolutely sure the site name is equal to the value of the dropdown? If there a value is passed that does not exist in the values of the dropdown, it will be blank. Try to do this: 1. Select a value in the dropdown manually 2. Run this in the console $("select[title='Project']").val(); 3. If you get undefined, you most likely do not have the correct title of the dropdown field 4. If you get the value you chose on the dropdown, do this in the console $("select[title='Project']").val("the name of the site here")

This is just to test if the problem is with the dropdown, or something else.

  • Thanks Anerd, will give it a try. is there anyway to make a sharepoint drop-down read-only using js/jq? – mdevm Jul 18 '18 at 14:43
  • Yes, you can use the .prop('disabled', true) or .attr('disabled') on the element, as so : $("select[title='Project']").prop('disabled', true). – Anerd Jul 18 '18 at 15:15
  • Hi @Anerd, I updated the code I am using for the requirement but for some reason its not setting the drop-down value to site name. I am getting the right site name, since I tested using alerting. FYI, the drop-down is a lookup column titled as 'Project Name'. – mdevm Jul 18 '18 at 16:55
  • I edited my answer instead, the text was not very readable as comment. – Anerd Jul 18 '18 at 17:04
  • Yes the site name is exactly same as drop-down value. – mdevm Jul 18 '18 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.