The script below allows a user to create a new folder in a SharePoint Document Library. The folder name is derived from the value that is entered into the text box ("txtFolderName") and a button ("btnSubmit") is pushed.

I want to add ANOTHER text field below the existing text box ("txtFolderName") called ("txtCompanyId"), whose value will set the field 'CompanyId' in the Document Library called 'SourcesLibrary'. In the code below, you'll see where I want to add the "txtCompanyId" text box. Both the Folder will be named, CompanyId field populated, and the Folder would be created on ("btnSubmit")

However, I don't know how to add the setting of the CompanyId field to my existing script, specifically whether or not to use folderCreateInfo.set. Is it possible? Does it require an entirely new function? I know the error is on my end. Any insight would be appreciated. Thanks.

Just for clarity:

  • SourcesLibrary: SharePoint List Name
  • CompanyId: Field Name in the SourcesLibrary List


<strong>Name Your Folder Below</strong><br /> 
<input type="text" id="txtFolderName" />

input type="text" id="txtCompanyId" would go here

<input type="button" id="btnSubmit" value="Create Folder" onClick="window.location.href=window.location.href;"/></div>
<div id="divResults"></div>


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


$(function () {



function bindButtonClick() {

$("#btnSubmit").on("click", function () {




function createFolder() {

var folderName = $("#txtFolderName").val();

var clientContext = new SP.ClientContext();

var oWebsite = clientContext.get_web();

var oList = oWebsite.get_lists().getByTitle("SourcesLibrary");

var folderCreateInfo = new SP.ListItemCreationInformation();



this.oListItem = oList.addItem(folderCreateInfo);




Function.createDelegate(this, this.onQuerySucceeded),

Function.createDelegate(this, this.onQueryFailed)



function onQuerySucceeded() {

$("#divResults").html("Folder successfully created!");


function onQueryFailed(sender, args) {

alert('Request failed. ' + args.get_message() +

'\n' + args.get_stackTrace());



You can set the CompanyId on the folder ListItem object itself, assuming CompanyId is a field in your list and txtCompanyId is a text input on your form. You would do so like this:

var companyId = $("#txtCompanyId").val();
this.oListItem = oList.addItem(folderCreateInfo);
this.oListItem.set_item("CompanyId", companyId);
  • Thanks for the quick and clear response. That worked, but now the CompanyId id being populated as "foo". I want it to be populated with the value in the text box whose id="txtCompanyId" – TheJDScott Jul 14 '17 at 21:29
  • Hi @TheJDScott Please concentrate on encouraging the good and helpful answers by clicking on Upvote especially when it help and guide you in the correct path :) once your issue is solved, in this case, you should Mark it as an answer! Thank you for your contributions :) – Mohamed El-Qassas MVP Jul 15 '17 at 0:10
  • @TheJDScott: Sorry, "foo" was just an example value. You'll need to use jQuery to grab the CompanyId similar to folderName. I'll update the answer to be more clear. :) – Jim Barntish Jul 15 '17 at 0:31
  • Thank you so much -- this worked perfectly. You really helped me out, and I appreciate your time. – TheJDScott Jul 15 '17 at 19:49

Your Answer

