Add SPUtility JS reference to the page.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="SPUtility.min.js"></script>
As we have different header in OData like verbose
, minimalmetadata
or nometadata
. Default header is verbose but you can change it.
var spJS = SPUtility();
// To Set minimalmetadata
spJS.setMinimalMetaData(true)
// To Set nometadata
spJS.setNoMetaData(true)
// To Set to default change it to false
spJS.setNoMetaData(false)
OR
spJS.setMinimalMetaData(false)
var spJS = SPUtility();
spJS.setSiteUrl("https://<siteName>.sharepoint.com")
var spJS = SPUtility();
spJS.getUserProperties('testUser@domain.com',"PropertyName").then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
spJS.getAllUserProperties('testUser@domain.com').then(function(data){
console.info(data);
},function(error){
console.error(error);
})
// Get all properties of current user
spJS.getAllPropertiesOfCurrentUser().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
spJS.web.currentUser().then(function(user){
console.info(user);
},function(error){
console.error(error);
})
var spJS = SPUtility();
spJS.web.ensureUser('testUser@domain.com').then(function(user){
console.info(user);
},function(error){
console.error(error);
})
var spJS = SPUtility();
spJS.web.getRoles().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
spJS.web.hasuniqueroleassignments().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var copyRoleAssignment = true, clearSubscope = false, spJS = SPUtility();
spJS.web.breakroleinheritance(copyRoleAssignment,clearSubscope).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
spJS.web.resetroleinheritance().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var groupIdorUserId = 1, roleDefId = 1, spJS = SPUtility();
spJS.web.addRoleAssignment(groupIdorUserId,roleDefId).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var groupIdorUserId = 1, spJS = SPUtility();
spJS.web.deleteRoleAssignment(groupIdorUserId).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
spJS.web.lists.getListByTitle("ListTitle").hasuniqueroleassignments().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
OR
var spJS = SPUtility();
spJS.web.lists.getListById("60a3a6aa-1947-4806-85e5-5d6718f194d3")
.hasuniqueroleassignments().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var copyRoleAssignment = true, clearSubscope = false, spJS = SPUtility();
spJS.web.lists.getListByTitle("ListTitle")
.breakroleinheritance(copyRoleAssignment,clearSubscope).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
OR
spJS.web.lists.getListById("60a3a6aa-1947-4806-85e5-5d6718f194d3")
.breakroleinheritance(copyRoleAssignment,clearSubscope).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var groupIdorUserId = 1, roleDefId = 1, spJS = SPUtility();
spJS.web.lists.getListByTitle("ListTitle")
.addRoleAssignment(groupIdorUserId,roleDefId).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
OR
spJS.web.lists.getListById("60a3a6aa-1947-4806-85e5-5d6718f194d3")
.addRoleAssignment(groupIdorUserId,roleDefId).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
spJS.web.lists.getListByTitle("ListTitle")
.resetroleinheritance().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
OR
spJS.web.lists.getListById("60a3a6aa-1947-4806-85e5-5d6718f194d3")
.resetroleinheritance().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var groupIdorUserId = 1, spJS = SPUtility();
spJS.web.lists.getListByTitle("ListTitle")
.deleteRoleAssignment(groupIdorUserId).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
OR
spJS.web.lists.getListById("60a3a6aa-1947-4806-85e5-5d6718f194d3")
.deleteRoleAssignment(groupIdorUserId).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
// Get Default display Form Url, similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle")
.getDefaultDisplayFormUrl().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
// Get Default edit Form Url, similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle")
.getDefaultEditFormUrl().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
// Get Default new Form Url, similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle")
.getDefaultNewFormUrl().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var allowContentTypes = true, baseTemplateId = 101, contentTypesEnabled = true,
listDescription = "List Descrition", listTitle = "ListTitle", spJS = SPUtility();
spJS.web.lists
.createList(allowContentTypes, baseTemplateId, contentTypesEnabled, listDescription, listTitle)
.then(function (data) {
console.info('List Created', data);
})
var spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").get().then(function (listObject) {
console.info('List object', listObject);
})
var spJS = SPUtility(), eTag = "'1'";
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").updateList({
'AllowContentTypes': false,
'ContentTypesEnabled': false,
'Description': 'Test Description',
}, eTag).then(function (updatedList) {
console.info('Updated List', updatedList);
})
var spJS = SPUtility(), eTag = "'1'";
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").deleteList(eTag).then(function (data) {
console.info('List Deleted', data);
})
var spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.select(["Title","Autor/Title","Autor/Id"])
.expand(["Author"]).topItem(100)
.filter("Title eq 'Test'")
.get().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.select(["Title","Autor/Title","Autor/Id"])
.expand(["Author"])
.filter("Title eq 'Test'")
.getAllItems().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.select(["Title","Autor/Title","Autor/Id"])
.expand(["Author"]).topItem(100)
.filter("Title eq 'Test'")
.getPaged().then(function(data,nextItem){
console.info(data);
if(nextItem){
nextItem.getNextItems().then(function(data,nextItem){
console.info(data);
},function(error){
console.error(error);
})
}
},function(error){
console.error(error);
})
var spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.select(["Title","Autor/Title","Autor/Id"])
.expand(["Author"])
.getItemById(1).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.addNewItem({Title: 'Test',LookupId: 23}).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var itemId = 1, eTag = "'1'", spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.updateItem(itemId, eTag, {Title: 'Test',LookupId: 23}).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var itemId = 1, eTag = "'1'", spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.deleteItem(itemId, eTag).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var itemId = 1, spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.hasuniqueroleassignments(itemId).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var itemId = 1, spJS = SPUtility(),
inputFiles = document.querySelector("#attachmentFile"), file = inputFile.files[0];
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.uploadAttachment(itemId, file).then(function (data) {
console.info(data);
},function(error){
console.error(error);
})
var itemId = 1, spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.getAttachments(itemId).then(function (data) {
console.info(data);
},function(error){
console.error(error);
})
var itemId = 1, spJS = SPUtility(), fileName = "Test.doc";
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.deleteAttachment(itemId, fileName).then(function (data) {
console.info(data);
},function(error){
console.error(error);
})
var itemId = 1, copyRoleAssignment = true, clearSubscope = false, spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.breakroleinheritance(itemId , copyRoleAssignment, clearSubscope)
.then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var itemId = 1, spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.resetroleinheritance(itemId).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var itemId = 1, groupIdorUserId = 1, roleDefId = 1, spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.addRoleAssignment(itemId, groupIdorUserId, roleDefId).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var itemId = 1, groupIdorUserId = 1, spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Items
.deleteRoleAssignment(itemId, groupIdorUserId).then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
//Similarly with get by list id.
spJS.web.lists.getListByTitle("ListTitle").Fields
.select(['Title','InternalName'])
.filter('Hidden eq false')
.get().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
spJS.web.siteColumns().Fields
.select(['Title','InternalName'])
.filter('Hidden eq false')
.get().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
var batch = new spJS.createNewBatch();
spJS.web.lists.getListByTitle('ListTitle').Items
.addNewItemUsingBatch({Title: 'Test'},batch)
var updateItemId = 2, itemETag = "*";
spJS.web.lists.getListByTitle('ListTitle').Items
.updateItemUsingBatch(updateItemId, itemETag, {Title: 'Test'},batch)
spJS.web.lists.getListByTitle('ListTitle').Items
.updateItemUsingBatch(40, "'1'", {Title: 'Test'},batch)
var deleteItemId = 3, deleteItemETag = "*";
spJS.web.lists.getListByTitle('ListTitle').Items
.deleteItemUsingBatch(updateItemId, itemETag, {Title: 'Test'},batch)
batch.executeBatch().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
spJS.web.siteGroups.groups
.select(['Title','Id'])
.filter("Id gt 20")
.getAllGroups().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
// By Group Name
spJS.web.siteGroups.groups.getByName("Group1")
.get().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
// By Group Id
spJS.web.siteGroups.groups.getById(10)
.get().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility();
// By Group Name
spJS.web.siteGroups.groups.getByName("Group1").users
.select(["Title","Id"])
.filter("Title eq 'User Name'")
.get().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
// By Group Id
spJS.web.siteGroups.groups.getById(10).users
.select(["Title","Id"])
.filter("Title eq 'User Name'")
.get().then(function(data){
console.info(data);
},function(error){
console.error(error);
})
var spJS = SPUtility(), folderServerRelativeUrl = "<siteServerRelative>/path/FolderName";
spJS.web.folder.GetFolderByServerRelativeUrl(folderServerRelativeUrl).then(function (folder) {
console.info('Folder Object',folder);
})
var spJS = SPUtility();
spJS.web.folder
.createFolder("<siteServerRelative>/Shared Documents/New FolderName").then(function (newFolder) {
console.info('New Folder', newFolder);
})
var spJS = SPUtility();
spJS.web.folder.listItemAllFields("<siteServerRelative>/path/FolderName").then(function (folder) {
console.info('Folder', folder);
})
var spJS = SPUtility(), eTag = '"1"';
spJS.web.folder
.renameFolder("<siteServerRelative>/path/FolderName","New Folder Name", eTag)
.then(function (folder) {
console.info('Folder Object', folder);
})
var spJS = SPUtility();
spJS.web.folder.getAllFilesFromFolder("<siteServerRelative>/path/FolderName").then(function (data) {
console.info('All Files', data);
})