intraactive-framework-jsomservice
v1.0.11
Published
JSOM calls for Sharepoint
Downloads
1
Keywords
Readme
IntraActive Framework - JSOM service
IntraActive Framework's "JSON service" package is a collection of functions to work with JSM for SharePoint
Installation
$ npm install intraactive-framework-jsomservice
Import
import { JSOMListService } from "intraactive-framework-jsomservice"
Usage
public getLists(): Promise<Array<SP.List>> {
return JSOMScriptLoader.EnsureSPJSOMScripts().then(() => {
return new Promise<any>((resolve, reject) => {
let clientContext: SP.ClientContext = new SP.ClientContext(this._currentWebUrl);
let web: SP.Web = clientContext.get_web();
let lists: SP.ListCollection = web.get_lists();
clientContext.load(lists);
clientContext.executeQueryAsync((sender: any, args: SP.ClientRequestSucceededEventArgs) => {
let listArray: Array<SP.List> = new Array<SP.List>();
var listEnumerator: IEnumerator<SP.List> = lists.getEnumerator();
while (listEnumerator.moveNext()) {
listArray.push(listEnumerator.get_current());
}
resolve(listArray);
}, (sender: any, args: SP.ClientRequestFailedEventArgs) => {
console.log("error", args);
reject(args);
});
});
});
}
public getList(id: string | SP.Guid): Promise<SP.List> {
if (id instanceof SP.Guid) {
return this.getListByGuid(id);
} else {
return this.getListByTitle(id);
}
}
public getListByGuid(guid: SP.Guid): Promise<SP.List> {
return JSOMScriptLoader.EnsureSPJSOMScripts().then(() => {
return new Promise<any>((resolve, reject) => {
let clientContext: SP.ClientContext = new SP.ClientContext(this._currentWebUrl);
let web: SP.Web = clientContext.get_web();
let list: SP.List = web.get_lists().getById(guid);
clientContext.load(list);
clientContext.executeQueryAsync((sender: any, args: SP.ClientRequestSucceededEventArgs) => {
resolve(list);
}, (sender: any, args: SP.ClientRequestFailedEventArgs) => {
console.log("error", args);
reject(args);
});
});
});
}
public getListByTitle(listName: string): Promise<SP.List> {
return JSOMScriptLoader.EnsureSPJSOMScripts().then(() => {
return new Promise<any>((resolve, reject) => {
let clientContext: SP.ClientContext = new SP.ClientContext(this._currentWebUrl);
let web: SP.Web = clientContext.get_web();
let list: SP.List = web.get_lists().getByTitle(listName);
clientContext.load(list);
clientContext.executeQueryAsync((sender: any, args: SP.ClientRequestSucceededEventArgs) => {
resolve(list);
}, (sender: any, args: SP.ClientRequestFailedEventArgs) => {
console.log("error", args);
reject(args);
});
});
});
}
public getListItems(list: SP.List, include?: string, camlQuery?: SP.CamlQuery): Promise<SP.ListItem[]> {
return JSOMScriptLoader.EnsureSPJSOMScripts().then(() => {
return new Promise<SP.ListItem[]>((resolve, reject) => {
let context: SP.ClientRuntimeContext = list.get_context();
if (!camlQuery) {
camlQuery = SP.CamlQuery.createAllItemsQuery();
}
let items: SP.ListItemCollection = list.getItems(camlQuery);
if (include) {
context.load(items, include);
} else {
context.load(items);
}
context.executeQueryAsync((sender: any, args: SP.ClientRequestSucceededEventArgs) => {
let itemEnumerator: IEnumerator<SP.ListItem> = items.getEnumerator();
let itemArray: Array<SP.ListItem> = new Array<SP.ListItem>();
while (itemEnumerator.moveNext()) {
itemArray.push(itemEnumerator.get_current());
}
resolve(itemArray);
}, (sender: any, args: SP.ClientRequestFailedEventArgs) => {
console.log("error", args);
reject(args);
});
});
});
}
public createListItem(list: SP.List, fieldValues: Map<string, any>): Promise<SP.ListItem> {
let context: SP.ClientRuntimeContext;
let itemCreateInfo: SP.ListItemCreationInformation;
let listItem: SP.ListItem;
return JSOMScriptLoader.EnsureSPJSOMScripts().then(() => {
context = list.get_context();
itemCreateInfo = new SP.ListItemCreationInformation();
listItem = list.addItem(itemCreateInfo);
return this.validateData(list, listItem, fieldValues);
}).then((item) => {
return new Promise<any>((resolve, reject) => {
item.update();
context.load(item);
context.executeQueryAsync((sender: any, args: SP.ClientRequestSucceededEventArgs) => {
resolve(listItem.get_id());
}, (sender: any, args: SP.ClientRequestFailedEventArgs) => {
console.log("error", args);
reject(args);
});
});
});
}
public deleteListItem(listItem: SP.ListItem): Promise<ListItem> {
return JSOMScriptLoader.EnsureSPJSOMScripts().then(() => {
return new Promise<any>((resolve, reject) => {
let context: SP.ClientRuntimeContext = listItem.get_context();
listItem.deleteObject();
context.load(listItem);
context.executeQueryAsync((sender: any, args: SP.ClientRequestSucceededEventArgs) => {
resolve();
}, (sender: any, args: SP.ClientRequestFailedEventArgs) => {
console.log("error", args);
reject(args);
});
});
});
}
public validateData(list: SP.List, listItem: SP.ListItem, fieldValues: Map<string, any>): Promise<SP.ListItem> {
let context = list.get_context() as SP.ClientContext;
return JSOMScriptLoader.EnsureSPJSOMScripts().then(() => {
return new Promise<any>((resolve, reject) => {
let oFields = list.get_fields();
let fixed: Map<string, any> = new Map<string, any>();
context.load(oFields);
context.executeQueryAsync((res, err) => {
let oFieldEnumerator: IEnumerator<SP.Field> = oFields.getEnumerator();
while (oFieldEnumerator.moveNext()) {
let oField: SP.Field = oFieldEnumerator.get_current();
let internalName: string = oField.get_internalName();
if (fieldValues.has(internalName)) {
switch (oField.get_typeAsString()) {
case "TaxonomyFieldTypeMulti": {
let values: Array<{ label: string, value: any }> = fieldValues.get(internalName);
if (values) {
let field: SP.Taxonomy.TaxonomyField = context.castTo(oField, SP.Taxonomy.TaxonomyField) as SP.Taxonomy.TaxonomyField;
let temp: Array<string> = new Array<string>();
values.forEach(v => {
temp.push("-1;#" + v.label + "|" + v.value);
});
let value: string = temp.join(";#");
let terms: SP.Taxonomy.TaxonomyFieldValueCollection = new SP.Taxonomy.TaxonomyFieldValueCollection(context, value, oField);
field.setFieldValueByValueCollection(listItem, terms);
}
break;
}
case "TaxonomyFieldType": {
let value: { label: string, value: any } = fieldValues.get(internalName);
if (value) {
let field: SP.Taxonomy.TaxonomyField = context.castTo(oField, SP.Taxonomy.TaxonomyField) as SP.Taxonomy.TaxonomyField;
var term: SP.Taxonomy.TaxonomyFieldValue = new SP.Taxonomy.TaxonomyFieldValue();
term.set_label(value.label);
term.set_termGuid(value.value);
term.set_wssId(-1);
field.setFieldValueByValue(listItem, term);
}
break;
}
case "UserMulti":
case "User": {
let values: [{ accountName: string }] = fieldValues.get(internalName);
let users: Array<SP.FieldUserValue> = new Array<SP.FieldUserValue>();
values.forEach((value) => {
if (value.accountName) {
users.push(SP.FieldUserValue.fromUser(value.accountName));
}
});
listItem.set_item(internalName, users);
break;
}
default: {
let value: string = fieldValues.get(internalName);
if (value) {
listItem.set_item(internalName, fieldValues.get(internalName));
}
break;
}
}
}
// console.log(oField.get_internalName() + " " + oField.get_required() + " " + oField.get_fieldTypeKind() + " " + oField.get_typeAsString() + " " + oField.get_typeDisplayName());
}
resolve(listItem);
});
});
});
}
public uploadFile(list: SP.List, fileName: string, fileContent: any): Promise<SP.File> {
var imgeType = fileContent.date;
let context: SP.ClientContext = list.get_context() as SP.ClientContext;
let web: SP.Web = context.get_web();
return JSOMScriptLoader.EnsureSPJSOMScripts().then(() => {
return new Promise<SP.File>((resolve, reject) => {
let folder: SP.Folder = list.get_rootFolder();
let createInfo: SP.FileCreationInformation = new SP.FileCreationInformation();
createInfo.set_url(fileName);
createInfo.set_overwrite(true);
createInfo.set_content(new SP.Base64EncodedByteArray());
let arr: any = this.convertDataURIToBinary(fileContent);
for (let i: number = 0; i < arr.length; ++i) {
createInfo.get_content().append(arr[i]);
}
let uploadedDocument: SP.File = folder.get_files().add(createInfo);
context.load(uploadedDocument);
context.executeQueryAsync((sender: any, args: SP.ClientRequestSucceededEventArgs) => {
resolve(uploadedDocument);
}, (sender: any, args: SP.ClientRequestFailedEventArgs) => {
console.log("error", args);
reject(args);
});
});
});
}
private convertDataURIToBinary(dataURI: any): any {
let BASE64_MARKER: string = ";base64,";
let base64Index: number = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
let base64: string = dataURI.substring(base64Index);
let raw: any = window.atob(base64);
let rawLength: number = raw.length;
let array: any = new Uint8Array(new ArrayBuffer(rawLength));
for (let i: number = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}
public getItemUrl(item: SP.ListItem): string {
let file: SP.File = item.get_file();
return this.getFileUrl(file);
}
public getFileUrl(file: SP.File): string {
let context: SP.ClientRuntimeContext = file.get_context();
let url: string = context.get_url();
url = url.substring(0, url.indexOf("/sites/"));
return url + file.get_serverRelativeUrl();
}