jQuery.ajax({
url: “https://<yoursite>/sites/<yourSite>/ABCteamSite/_api/web”,
type: “POST”,
data: JSON.stringify({‘__metadata’: {‘type’: ‘SP.Web’ },’Description’: ‘ABCteamSite’,’RequestAccessEmail’: ‘<youraddress>@gmail.com’}),
headers: {
“accept”: “application/json; odata=verbose”,
“content-type”:”application/json;odata=verbose”,
“X-RequestDigest”: jQuery(“#__REQUESTDIGEST”).val(),
“X-HTTP-Method”: “MERGE”
}
}).done(function (data) {
console.log(“Done”);
}).fail(function (error) {
console.log(“failed”);
});
Creating random value via REST API
lastDigYear = Math.floor((Math.random() * 6) + 1);
lastDigMonth = Math.floor((Math.random() * 8) + 1);
AllDigDay = Math.floor((Math.random() * 15) + 1);
TolastDigYear = lastDigYear + 1;
TolastDigMonth = lastDigMonth + 1;
ToAllDigDay = AllDigDay + 3;
var data2 = {
__metadata: { ‘type’: ‘SP.Data.Form70ListItem’ },
Title: ‘Please provide title here’,
ReportingPeriodFrom: ‘201’+lastDigYear.toString()+’-0’+lastDigMonth.toString()+’-‘+AllDigDay.toString()+’T15:18:21.135Z’,
ReportingPeriodTo: ‘201’+TolastDigYear.toString()+’-0’+TolastDigMonth.toString()+’-‘+ToAllDigDay.toString()+’T15:18:21.135Z’,
PartSources: Math.floor((Math.random() * 100) + 1),
ActivePermits: Math.floor((Math.random() * 100) + 1),
InitialPermitsPeriod: Math.floor((Math.random() * 100) + 1),
InitialPermits18Months: Math.floor((Math.random() * 100) + 1),
RegionId: Math.floor((Math.random() * 10) + 1),
PermitAuthId: Math.floor(Math.random()*(158-42+1)+42),
OutstandingInitialApps: Math.floor((Math.random() * 100) + 1),
OutstandingRenewalPermits: Math.floor((Math.random() * 100) + 1),
SignificantModPeriod: Math.floor((Math.random() * 100) + 1),
SignificantMods18Months: Math.floor((Math.random() * 100) + 1),
OutstandingSignficantMods: Math.floor((Math.random() * 100) + 1),
Form70comments: ‘Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden’,
};
$.ajax({
url: “/_api/web/lists/GetByTitle(‘Form70’)/items”,
type: “POST”,
data: JSON.stringify(data2),
headers: {
“X-RequestDigest”: $(“#__REQUESTDIGEST”).val(),
“accept”: “application/json; odata=verbose”,
“content-type”: “application/json;odata=verbose”
}
}).done(function () {
console.log(“Done”);
}).fail(function () {
console.log(“FaiL”);
});
REST API roledefinitions
jQuery.ajax({
url: “https://usepa.sharepoint.com/sites/OAR_Custom/_api/web/roledefinitions“,
type: “GET”,
headers: {
“accept”:”application/json;odata=verbose”
}
}).done(function (data) {
console.log(“Done”);
jQuery.each( data.d.results, function( key, value ) {
console.log(value.PrincipalId);
});
}).fail(function (error) {
console.log(error);
});
Adding Multiple value to lookup field via REST
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”) }
);
Getting Started: Cloud Business App in SharePoint Online
Getting Started: Authentication using JavaScript
This example does not use AngularJS or/and ADAL. There is no UI. The functions need to be run from the console and the access token extracted from the URL. A Azure active directory application was create to provide the client id. It works like:
Construct a URL that take the user to a log-in screen
Once the user log in an access token is returned in the URL
That access token is extracted and used in the REST request to get the SharePoint Online resources
(Interestingly you are calling cross domain and not using SP.RequestExecutor)
VSO project JavaScript Auth
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
Client Side Rendering: Grabbing all fields on using OnPostRender
Here is a Visual Studio Project that has demos of various ways of using client side rendering including this one:
In this example I modified the schema.xml file to and created a CustomNewForm.aspx. Remember to set Deployment Type to ElementFile. The code in CSRExample1.js is meant to grab all the field on after the CustomNewForm.aspx has rendered.
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”);
}
});
How do I: Populate a drop-down
The schema.xml will look like:
<Field Name=”NomStatus” ID=”{6f37ad73-3881-4411-bb98-b2f4dd0273b0}” DisplayName=”Status” Format=”Dropdown” Type=”Choice”>
<CHOICES>
<CHOICE>Pending Approval</CHOICE>
<CHOICE>Approved</CHOICE>
<CHOICE>Rejected</CHOICE>
</CHOICES>
</Field>
The elements.xml will look like:
<Field Name=”NomStatus”>Approved</Field>