0

I have a SharePoint Add-In that is examining files in document libraries across a farm. These document libraries ostensibly share a Managed Metadata site column, which I hope to be able to extract Custom Properties of.

However, I have a problem when introducing SP.Taxonomy.js. I have no problem in getting back an anonymous object of the term when this library is not included in the project.

//In SPContext (Singleton class)
public static GetItemDetail(itemurl: string): Promise<SP.ListItem> {
    return new Promise<SP.ListItem>((resolve, reject) => {
        SPContext.WebFromNewHost(itemurl).then(contentWeb => {
            let itemPath: string = new URL(itemurl).href.replace(contentWeb.get_url(), '');
            let itemRelativeUrl = decodeURIComponent(`${contentWeb.get_serverRelativeUrl()}${itemPath}`);
            let file: SP.File = contentWeb.getFileByServerRelativeUrl(itemRelativeUrl);
            SPContext.ClientContext.load(file, 'ListItemAllFields');
            SPContext.ClientContext.executeQueryAsync(
                (sender: any, args: SP.ClientRequestSucceededEventArgs) => {
                    resolve(file.get_listItemAllFields());
                },
                (sender: any, args: SP.ClientRequestFailedEventArgs) => {
                    reject(args.get_message());
                }
            );
        });
    });
}

//In File class
SPContext.GetItemDetail(self.Uri).then((item: SP.ListItem) => {
    let fieldValues = item.get_fieldValues();
    let reportType: SP.Taxonomy.TaxonomyFieldValue = fieldValues[reportTypeColumn];
    debugger;
    //...
});

At the debugger, Chrome gives:

reportType: Object
  Label: "Sales"
  TermGuid: "5a3b7d93-2bb4-4fef-b7a0-b870205e1d18"
  WssId: 3
  _ObjectType_: "SP.Taxonomy.TaxonomyFieldValue"
  _proto_: Object

However, if Sp.Taxonomy.js is included, the debugger point gives:

reportType: SP.Taxonomy.TaxonomyFieldValue
  _proto_: Object
    &0_1: null
    $1_1: null
    $2_1: 0
    ...

In this case, it appears that the reportType column is unable to extract the term. All calls to reportType.get_label() etc return null as well.

I did try to call context.load(reportType) to see if it is now something I needed to execute explicitly; however SP.Taxonomy.TaxonomyFieldValue is not also an SP.ClientObject and can't be loaded (or at least, throws an error in TypeScript).


Just in case it's important, here's SPContext.WebFromNewHost(), which resets the appContextSite to retrieve information from an arbitrary site collection in the farm:

public static WebFromNewHost(hostweburl: string): Promise<SP.Web> {
    if (SPContext._instance._clientContext.get_webRequestExecutorFactory() == null) {
        let factory: SP.ProxyWebRequestExecutorFactory = new SP.ProxyWebRequestExecutorFactory(SPContext.AppWebUrl);
        SPContext._instance._clientContext.set_webRequestExecutorFactory(factory);
    }
    let appContextSite = new SP.AppContextSite(SPContext.ClientContext, hostweburl);
    let web: SP.Web = appContextSite.get_web();
    SPContext.ClientContext.load(web);

    return new Promise<SP.Web>((resolve, reject) => {
        SPContext.ClientContext.executeQueryAsync(
            (sender: any, args: SP.ClientRequestSucceededEventArgs) => {
                resolve(web);
            },
            (sender: any, args: SP.ClientRequestFailedEventArgs) => {
                reject(args.get_message());
            }
        );
    });
}
0

I appear to have found a solution, although I don't quite understand it!

Originally, the taxonomy library was loaded via a plain script tag:

<script type="text/javascript" src="/_layouts/15/SP.RequestExecutor.js"></script>
<script type="text/javascript" src="/_layouts/15/SP.Taxonomy.js"></script>

However, swapping this for

<SharePoint:ScriptLink name="sp.RequestExecutor.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink name="sp.Taxonomy.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />

and modifying my start file (knockout.js initialisation) to:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext',
    () => SP.SOD.executeFunc("userprofile", 'SP.UserProfiles',
        () => SP.SOD.executeFunc("sp.requestexecutor.js", null,
            () => SP.SOD.executeFunc("sp.taxonomy.js", null,
                () => ko.applyBindings(new RootViewModel(), document.getElementById("content"))
            )
        )
    )
);

and now the resulting TaxonomyFieldValues are appearing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.