0

我正在 Sharepoint 2016 中试用 JSOM。我制作了一个包含以下代码的 WebPart -

<div id="user-output"></div>
Movie Title: <input type="text" id="movie-title" /><br />
Description: <input type="text" id="movie-description" /><br />
<button type="button" id="submit-button">Add Movie</button>

<div id="movies-output"></div>

<script type="text/javascript" src="/SiteAssets/jquery-3.2.1.min.js"></script>
<script type="text/ecmascript" src="../_layouts/15/SP.UserProfiles.js"></script>

<script type="text/javascript">
    $(function () {

        $('#submit-button').on('click', function () {
            var context = SP.ClientContext.get_current();
            var movies = context.get_web().get_lists().getByTitle('Movies');
            var movieCreationInfo = new SP.ListItemCreationInformation();
            var movie = movies.addItem(movieCreationInfo);
            movie.set_item("Title", $('#movie-title').val());
            movie.set_item("MovieDescription", $('#movie-description').val());
            movie.update();
            context.load(movie);

            context.executeQueryAsync(success, failure);
        });

        function success() {
            $('#movies-output').text('Created movie!');
        }

        function failure() {
            $('#movies-output').text('Something went wrong');
        }

        var upp;

        // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
        //SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
        SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', getUserProperties);
        //SP.SOD.executeFunc('SP.UserProfiles.js', getUserProperties);

        function getUserProperties() {

            // Get the current client context and PeopleManager instance.
            var clientContext = new SP.ClientContext.get_current();
            var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

            upp = peopleManager.getMyProperties();
            clientContext.load(upp, 'UserProfileProperties');
            clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
        }

        // This function runs if the executeQueryAsync call succeeds.
        function onRequestSuccess() {
            $('#user-output').html('User Name: ' + upp.get_userProfileProperties()['PreferredName'] +
                '<br/>Department: ' + upp.get_userProfileProperties()['Department'] +
                '<br/>Designation: ' + upp.get_userProfileProperties()['Title'] +
                '<br/>Employee ID: ' + upp.get_userProfileProperties()['EmployeeID'] +
                '<br/>Branch Code: ' + upp.get_userProfileProperties()['branchCode']
                );
        }

        // This function runs if the executeQueryAsync call fails.
        function onRequestFail(sender, args) {
            $('#user-output').text("Error: " + args.get_message());
        }
    });

这段代码的作用是 -

  1. user-output在文档加载就绪时在 div 中显示用户信息
  2. 单击添加电影按钮时保存电影记录

但是,由于某种原因,当单击“添加电影”按钮时,代码会添加两部电影而不是一部电影。我认为这与ClientContext. 但我不知道为什么,或者如何解决它。任何人都可以帮忙吗?

4

1 回答 1

0

我不确定它是如何发生的,或者它是否是一个错误,但是在摆弄页面源以找出为什么会发生重复发布时,我看到我的 Web 部件代码在页面中被渲染了两次。一部分是可见的,另一部分在隐藏的 div 下。但是,当我去编辑页面删除隐藏的 Web 部件时,我不能。所以我将我的页面恢复为模板版本并重新添加了 Web 部件。之后,Web 部件正常工作。代码没有问题。

于 2017-08-10T11:23:14.940 回答