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
HTML
<div>
<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>
JS
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"</script>
<script>
$(function () {
bindButtonClick();
});
function bindButtonClick() {
$("#btnSubmit").on("click", function () {
createFolder();
});
}
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();
folderCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
folderCreateInfo.set_leafName(folderName);
this.oListItem = oList.addItem(folderCreateInfo);
this.oListItem.update();
clientContext.load(oList);
clientContext.executeQueryAsync(
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());
}
</script>