var itemProperties = {
‘__metadata’: { “type”: “SP.Data.QuestionsListItem” },
‘AnsChoicesId’: { “results”: [1,3] } //multi-valued User field value
};
$.ajax({
url: appweburl + “/_api/web/lists/GetByTitle(‘ListTitleGoesHere’)/items(“+ListItem+”)”,
type: “MERGE”,
data: JSON.stringify(itemProperties),
headers: {
“X-RequestDigest”: $(“#__REQUESTDIGEST”).val(),
“accept”: “application/json; odata=verbose”,
“If-Match”: “*”,
“content-type”: “application/json;odata=verbose”
}
}).done(function () {
console.log(“Done”);
}).fail(function () {
console.log(“FaiL”);
});
In the above code all of the array items will be replaced with the item ID’s specified.
//code coming soon
Here is what adding Multiple values looks like in JSOM (This example is create a new record, not updating a record)
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle(‘ListTitleGoesHere’);
var itemCreateInfo = new SP.ListItemCreationInformation();
oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item(‘Title’, ‘HelloThisStuffWorld’);
var lookupsIds = [1, 2, 3];
var lookups = [];
for (var ii in lookupsIds) {
var lookupValue = new SP.FieldLookupValue();
lookupValue.set_lookupId(lookupsIds[ii]);
lookups.push(lookupValue);
}
oListItem.set_item(‘AnsChoices’, lookups);
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(
function () { alert(“Success!”) },
function () { alert(“Request failed”) }
);
Category: JavaScript
Authentication to SharePonit Online using AngularJS and ADAL.js
This project demo how to user AngularJS and ADAL.js to authenticate and read date from a SharePoint Online list. An Azure active directory application was create first, and then and empty ASP.Net web application was created via Visual Studio 2015 community. Project can be found here: ADAL.js and AngularJS
Create and add data to a file in SharePoint Online using JavaScript
$(document).ready(function () {
clientContext = SP.ClientContext.get_current();
$(“#btnPublish”).click(function () {
getSPItems();
});
function getSPItems() {
var spList = clientContext.get_web().get_lists().getByTitle(‘Team_Members’);
var camlQuery = new SP.CamlQuery();
listItems = spList.getItems(camlQuery);
clientContext.load(listItems);
clientContext.executeQueryAsync(saveHtmlFile, onFailure);
}
function saveHtmlFile() {
// var fileContent = uiContext.getJSContent();
var fileContent = “”;
Enum = listItems.getEnumerator();
while (Enum.moveNext()) {
GetTitle = Enum.current.get_fieldValues();
fileContent += ” <div style=’position:relative;padding-bottom:10px’><img src='” + GetTitle.Image.$1_1 + “‘ alt=’employee Images’ style=’position:absolute’ height=’87’ width=’87’> <ul style=’padding-left:125px;list-style-image:url(../Images/tinyArrow.gif)’><li style=’padding-bottom:5px’><span style=’font-weight:bold’>” + GetTitle.Title + “</span></li><li style=’padding-bottom:5px’><span style=’font-weight:bold’>” + GetTitle.Position + “</span></li><li style=’padding-bottom:5px’><span style=’font-weight:bold’>” + GetTitle.Department1 + “</span></li><li class=’lastInRow’ style=’padding-bottom:5px’>” + GetTitle.Bio + “</li></ul></div>”;
}
var jsContent = “(function () {“
+ “$(‘#divContent’).html(“” + fileContent + “”);”
+ “})();”;
var filesLibrary = clientContext.get_web().get_lists().getByTitle(“JSCompile”);
var fileCreateInfo = new SP.FileCreationInformation();
fileCreateInfo.set_url(“publish.js”);
fileCreateInfo.set_overwrite(true);
fileCreateInfo.set_content(new SP.Base64EncodedByteArray());
for (var i = 0, fileLength = jsContent.length ; i < fileLength; ++i) {
fileCreateInfo.get_content().append(jsContent.charCodeAt(i));
}
// Upload the file to the root folder of the document library
this.newFile = filesLibrary.get_rootFolder().get_files().add(fileCreateInfo);
clientContext.load(this.newFile);
clientContext.executeQueryAsync(updateContentVersionNumber, onFailure);
}
function onFailure(sender, args) {
alert(“Publish Failed: ” + args.get_message() + “nIf this error continues please contact: thesharepointhelper@gmail.com”);
}
function updateContentVersionNumber() {
alert(“Publish Successful”);
}
});
JavaScript Object Model: Add user to group
var clientContext = new SP.ClientContext();
var groupCollection = clientContext.get_web().get_siteGroups();
// Get the visitors group, assuming its ID is 4.
visitorsGroup = groupCollection.getById(4);
user = clientContext.get_web().get_currentUser();
var userCollection = visitorsGroup.get_users();
userCollection.addUser(user);
clientContext.load(user);
clientContext.load(visitorsGroup);
clientContext.executeQueryAsync();
SharePoint 2013: Get the email address of current logged on user
ExecuteOrDelayUntilScriptLoaded(getWebUserData, “sp.js”);
function getWebUserData() {
context = new SP.ClientContext.get_current();
web = context.get_web();
currentUser = web.get_currentUser();
currentUser.retrieve(); // with out this the get_title() on success call back will not be initialized
context.load(web);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
Function.createDelegate(this, this.onFailureMethod));
}
function onSuccessMethod(sender, args) {
var userObject = web.get_currentUser();
var UserEmail = userObject.get_email();
console.log(UserEmail);
}
function onFailureMethod(sender, args) {
alert(‘request failed Please contact McFarland.Jeffrey@epa.gov’);
}
SharePoint Designer 2010 JavaScript alert function
How do I: Create a simple form to collect data in SharePoint 2010
This was done using a SharePoint Online site.
There is an important piece of JavaScript code that allows you to re-direct your page once the “Save” button is click:
<input type=”button” value=”Save” class=”btnStyle” name=”btnSave” onclick=”javascript: {ddwrt:GenFireServerEvent(‘__commit;__redirect={http://sharepointsource123-web.sharepoint.com/CRT005/SitePages/RedirectLanding1.aspx}’)}” />
Create a SharePoint 2010 list using JavaScript
Create a SharePoint 2010 list using JavaScript client object model. Get the .wsp here.
Using SharePoint controls inside of SharePoint Designer 2010
Client Object Model: SP.UI.ModalDialog.showModalDialog(options)
SharePoint 2010 has introduced a client object model. Now you can use JavaScript to access SharePoint data. Visit MSDN to have a look at the JavaScript API
http://msdn.microsoft.com/en-us/library/ee538253.aspx