npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

intraactive-framework-jsomservice

v1.0.11

Published

JSOM calls for Sharepoint

Downloads

1

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();
    }