0

I am currently working on an Office add-in that will check a word doc for content controls, and will take the name and tag of those content controls and make a rest call to SharePoint to check Mod Dates. I am currently loading the tag and title with out and issue, my question is if I find that the mod dates are not the same, I would like to be able to load that specific Content Control.

Current Code

function loadContentControls() {
Word.run(function (context) {
    var contentControlProperties = [];
    var contentControls = context.document.contentControls;
    context.load(contentControls, "id");
    return context.sync().then(function () {
        if (contentControls.items.length > 0) {
            for (var x = 0; x < contentControls.items.length; x++) {
                contentControls.items[x].load('title,' + "tag");
            }
        }
        else {
            $("#notificationBody").html("<h4>No Update Found</h4>");
            messageBanner.showBanner();
            messageBanner.toggleExpansion();
        }
        return context.sync().then(function (e) {
            for (var x = 0; x < contentControls.items.length; x++) {
                contentControlProperties.push({
                    Name: contentControls.items[x].title,
                    Moddate: contentControls.items[x].tag,
                });
            }
            return context.sync().then(function () {
                var url;
                var unParsedDateTime;
                var parsedDateTime;
                for (var i = 0; i < contentControlProperties.length; i++) {
                    url = "https://tenant/sites/ContentCenter/_api/web/Lists/GetByTitle('kist')/items?select=Title,Title&$filter=Title eq '" + contentControlProperties[0].Name + "'";
                    authContext.acquireToken(config.endpoints.SharePoint, function (error, token) {
                        if (error || !token) {
                            console.log('error');
                            return;
                        }
                        else {
                            $.ajax({
                                type: "GET",
                                url: url,
                                headers: {
                                    "Authorization": "Bearer " + token,
                                    "accept": "application/json;odata=verbose"
                                },
                                success: function (data) {
                                    unParsedDateTime = new Date(data.d.results[0].Modified);
                                    parsedDateTime = unParsedDateTime.toLocaleDateString('en-US').concat(' ' + unParsedDateTime.getHours() + ':' + unParsedDateTime.getMinutes());

                         >> So if there is a date discrepancy I would like to grab that specific content control here  

                                },
                                error: function (error) {
                                    console.log("Fetching list from SharePoint failed.");
                                }
                            })

                        }
                    });
                }
            })
        })
    })
    .catch(function (error) {
        error.ErrorLocation = "Inserting Content To Doc";
        error.ErrorCode = error.debugInfo.errorLocation;
        error.ErrorMessage = "Could Not Insert Image";
        error.Selection = selectedContents.Name;
        ErrorHandler(error);
    })
})
}    

1 Answer 1

0

I was unable to actually get that specific content control once it was loaded even by id, which I thought would have been possible. So I went ahead and made a deffered item which I am calling inside the my context.sync().then(). The way this would work is When my Token get back from adal I will go ahead and execute the for loop. This will ensure that my response from SharePoint will be in order, as the code below will show. So instead of going back and actually loading the specific content control I can keep it all in the ajax call, and when the two time are different I can give that specific content control a red background.

 return context.sync().then(function (e) {
            $.when(TokenForSharePoint()).then(function (sharepointToken) {
                if (sharepointToken === "Error") {
                    authContext.login();
                }
                else {
                    for (var x = 0; x < contentControls.items.length; x++) {
                        itemUrl = "https://tenat.com/sites/*site*/_api/web/Lists/GetByTitle('*list*')/items?select=Title,Title&$filter=Title eq '" + contentControls.items[x].title + "'";
                        $.ajax({
                            type: "GET",
                            async: false,
                            url: itemUrl,
                            headers: {
                                "Authorization": "Bearer " + sharepointToken,
                                "accept": "application/json;odata=verbose"
                            },
                            success: function (data) {      
                                var localDocTest = new Date(data.d.results[0].Modified);
                                var spText = new Date(contentControls.items[0].tag);

                                if (localDocTest != spText) {
                                    // I will highlight the content control
                                }                                    
                            },
                            error: function (error) {
                                console.log("Fetching list from SharePoint failed.");
                            }
                        })
                    }                      
                }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.