从mySite获取myTasks

时间:2016-07-05 07:27:41

标签: javascript sharepoint sharepoint-2013

您好我尝试从sharepoint 2013获取当前用户的myTasks。我尝试使用Rest-API和JSON,但我总是收到错误。根据调试器ExecuteOrDelayUntilScriptLoaded is undefined

这是我到目前为止的小Javascript:

<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>

所以任何帮助或建议都会很棒。如果你想我也可以用rest-api发布我的代码,但错误保持不变。

感谢您的帮助和时间。

1 个答案:

答案 0 :(得分:0)

以下是使用javascript在html文件中查询我网站上的任务的方法。最重要的是以正确的顺序加载src文件并仅使用jquery-min文件而不是普通的jQuery文件。

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

here是一篇很好的小博客文章,其中包含所有可能性: