Andreas A. Andreas A. - 3 months ago 32
Javascript Question

get myTasks from mySite

Hello i try to get myTasks for the current User from sharepoint 2013. I tried it with the Rest-API and with JSON but i always get an error. Acording to the debugger

ExecuteOrDelayUntilScriptLoaded is undefined
.

Here is my little Javascript i got so far:

<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.0/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/knockout/knockout-2.1.0.js"></script>
<script type="text/javascript" src="/sps10/EDV/jquery.js"></script>

<script type="text/javascript">

var context, userSessionManager ,userSession , query , myTasks ;
$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(function () {
context = SP.ClientContext.get_current();
ExecuteOrDelayUntilScriptLoaded(function () {
userSessionManager = new SP.WorkManagement.OM.UserOrderedSessionManager(context);
userSession = userSessionManager.createSession();
query = new SP.WorkManagement.OM.TaskQuery(context);
myTasks = userSession.readTasks(query);
}, "sp.workmanagement.js");
getMyTasks();
}, "sp.js");
});

function getMyTasks() {
context.load(myTasks);
context.executeQueryAsync(onGetMyTasksSuccess, onGetMyTasksFail);
}

// This function is executed if the above call is successful
function onGetMyTasksSuccess() {
console.log("Successfully retrieved tasks...");
var taskEnumerator = myTasks.getEnumerator();
while (taskEnumerator.moveNext()) {
var task = taskEnumerator.current;
console.log("Task: " + task.get_id() + " - " + task.get_name());
}
}

// This function is executed if the above call fails
function onGetMyTasksFail(sender, args) {
console.log('Failed to get tasks. Error:' + args.get_message());
}


</script>


So any help or advise would be great. If you want i can post my Code with the rest-api too but the error stays the same.

Thanks for your help and time.

Answer

Here is now the way to query the tasks from my site in a html file with javascript. The most important thing is to load the src file in the right order and to use only the jquery-min file not the normal jQuery file.

    <script src="/_layouts/15/init.js" type="text/javascript"></script>
    <script src="/_layouts/15/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="/_layouts/15/sp.core.js" type="text/javascript"></script>
    <script src="/_layouts/15/sp.runtime.js" type="text/javascript"></script>
    <script src="/_layouts/15/sp.js" type="text/javascript"></script>
    <script src="/_layouts/15/sp.workmanagement.js" type="text/javascript"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js" type="text/javascript"></script>

        <script type="text/javascript">

            'use strict';

            var context = new SP.ClientContext.get_current();
            var userSessionManager = new SP.WorkManagement.OM.UserOrderedSessionManager(context);
            var userSession = userSessionManager.createSession();
            var query = new SP.WorkManagement.OM.TaskQuery(context);
            var myTasks = userSession.readTasks(query);
            var tasks = [];


                getMyTasks();


            function getMyTasks() {
                context.load(myTasks);
                context.executeQueryAsync(onGetMyTasksSuccess, onGetMyTasksFail);
            }

            function onGetMyTasksSuccess() {
                console.log("Successfully retrieved tasks...");
                var taskEnumerator = myTasks.getEnumerator();
                while (taskEnumerator.moveNext()) {
                    var task = taskEnumerator.current;
                    console.log("Task: " + task.get_id() + " - Taskname: " + task.get_name() + " - Beschreibung: " + task.get_description() + " - dueDatum: " + task.get_dueDate() + " - Startdatum: " + task.get_startDate() + " - Persönlich: " + task.get_isPersonal() + " - Fertiggestellt: " + task.get_isCompleted());
                    tasks.push({
                        taskName: task.get_name(),
                        description: task.get_description(),
                        dueDate: task.get_dueDate(),
                        startDate: task.get_startDate(),
                        personally: task.get_isPersonal(),
                        complete: task.get_isCompleted()
                    });

                }

                console.log(tasks);

            }


            // This function is executed if the above call fails
            function onGetMyTasksFail(sender, args) {
                console.log('Failed to get tasks. Error:' + args.get_message());
            }

And here is a nice little blog post with all the possibilities: