Sharepoint 2013 App AngularJS:添加到列表时导致错误的查找字段“模型

时间:2015-09-11 18:17:11

标签: angularjs sharepoint sharepoint-2013

我有一个Players列表,其中Team Name的查找字段链接到Teams列表。

尝试添加或更新播放器列表时,我收到错误

  

“模型无法解析名为'SP.Data.TeamsListItem'的类型。当模型可用时,每个类型名称必须解析为有效类型。”

球员名单的指南是'16a03b71-07ea-49c4-ab14-419b51047119'。

球队名单的指导是'2a2916c2-22fb-4588-bca1-83479a4d85b2'。

发生错误时,这是​​数据字段的内容:

<pre>
    "{\"__metadata\":{\"type\":\"SP.Data.PlayersListItem\"},\"Title\":\"Bill\",\"PlayerPosition\":\"Goalie\",\"Birthdate\":\"2/21/1999\",\"TeamName\":{\"__metadata\":{\"id\":\"Web/Lists(guid'2a2916c2-22fb-4588-bca1-83479a4d85b2')/Items(1)\",\"uri\":\"https://app-2971fdcf9ede6a.rawtecapps.com/sites/raiders/AngularCRM/_api/Web/Lists(guid'2a2916c2-22fb-4588-bca1-83479a4d85b2')/Items(1)\",\"etag\":\"\\\"1\\\"\",\"type\":\"SP.Data.TeamsListItem\"},\"Id\":1,\"Title\":\"aa\",\"ID\":1,\"$$hashKey\":\"object:176\"},\"Season\":\"2015\"}"
</pre>

我的查找字段的定义。

<pre>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
      <Field
           ID="{7853bbf2-9fd7-4f85-aca8-269d8f967650}"
           Name="TeamName"
           DisplayName="Team Name"
           Type="Lookup"
           List="Lists/Teams"
           ShowField="Title"
           Required="FALSE"
           Group="Custom Site Columns">
      </Field>
    </Elements>
</pre>

团队列表的内容类型:

<pre>
     <Elements xmlns="http://schemas.microsoft.com/sharepoint/"><ContentType ID="0x0100A379177A3533447D9B6546708F7DFD2D" Name="Teams" Group="Custom Content Types" Description="Teams Content Type" Inherits="FALSE" Version="0">
    <FieldRefs>
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" DisplayName="Team Name" Required="TRUE" Name="Title" />
      <FieldRef ID="{fce82853-3f44-4cc9-8f71-eb6506be0e9e}" DisplayName="Team City" Required="FALSE" Name="TeamCity" />
    </FieldRefs>
  </ContentType>
</pre>

playersnew.html页面:

<pre>
     <div class="form-horizontal">
    <fieldset>
        <div class="form-group">
            <label for="txtID" class="col-lg-2 control-label">ID:</label>
            <div class="col-lg-6">
                <input id="txtID" type="text" readonly class="form-control" ng-model="player.ID">
            </div>
        </div>
        <div class="form-group">
            <label for="txtPlayerName" class="col-lg-2 control-label">Player Name:</label>
            <div class="col-lg-6">
                <input id="txtPlayerName" type="text" class="form-control" ng-model="player.Title">
            </div>
        </div>
        <div class="form-group">
            <label for="txtPlayerPosition" class="col-lg-2 control-label">Player Position:</label>
            <div class="col-lg-6">
                <input id="txtPlayerPosition" type="text" class="form-control" ng-model="player.PlayerPosition">
            </div>
        </div>
        <div class="form-group">
            <label for="txtBirthdate" class="col-lg-2 control-label">Birthdate:</label>
            <div class="col-lg-6">
                <input id="txtBirthdate" type="text" class="form-control" ng-model="player.Birthdate">
            </div>
        </div>
        <div class="form-group">
            <label for="txtTeamName" class="col-lg-2 control-label">Team Name:</label>
            <div class="col-lg-6" ng-controller="TeamDropDownController">
                <select name="teams" ng-model="player.TeamName" ng-options="team.Title for team in teams">
                    <option ng-repeat="team in teams" value="{{team.Id}}">{{team.Title}}</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label for="txtSeason" class="col-lg-2 control-label">Season:</label>
            <div class="col-lg-6">
                <input id="txtSeason" type="text" class="form-control" ng-model="player.Season">
            </div>
        </div>
        <div class="form-group">
            <div class="col-lg-offset-2">
                <input id="cmdSave" type="button" class="button" value="Save" data-ng-click="addPlayer()" />
            </div>
        </div>
</pre>

Angular JS(playersnew Controller):

 ><pre> 
 app.controller('playersnewController',
     function ($scope, $location, wingtipCrmService) {
         $scope.player = {};
         $scope.player.Title = "";
         $scope.player.PlayerPosition = "";
         $scope.player.Birthdate = "";
         $scope.player.TeamName = "";
         $scope.player.TeamName.Id = "";
         $scope.player.TeamName.Title = "";
         $scope.player.Season = "";
         $scope.addPlayer = function () {
             var playerName = $scope.player.Title;
             var playerPosition = $scope.player.PlayerPosition;
             var birthdate = $scope.player.Birthdate;
            var teamName = $scope.player.TeamName;
             var teamNameId = $scope.player.TeamName.Id;
             var teamNameTitle = $scope.player.TeamName.Title;
             var season = $scope.player.Season;
             wingtipCrmService.addPlayer(playerName, playerPosition, birthdate, teamName, teamNameId, teamNameTitle ,season)
               .success(function (data) {
                   $location.path("/playershome");
               });
         }
     } );
</pre>

Angular JS(addplayer服务):

>     <pre>
    service.addPlayer = function (PlayerName,PlayerPosition,Birthdate,TeamName,TeamNameId,TeamNameTitle,Season) {
    var restQueryUrl = "../_api/web/lists/getByTitle('Players')/items";
    var playerData = {
        __metadata: { "type": "SP.Data.PlayersListItem" },
        Title: PlayerName,
        PlayerPosition: PlayerPosition,
        Birthdate: Birthdate,
        TeamName: TeamName,
        Season: Season
    };
    var requestBody = JSON.stringify(playerData);
    return $http({
                       method: 'POST',
              url: restQueryUrl,
              contentType: "application/json;odata=verbose",
              data: requestBody,
              headers: {
                  "Accept": "application/json; odata=verbose",
                  "X-RequestDigest": requestDigest,
                  "content-type": "application/json;odata=verbose"
              }
          });
      }
</pre>

Angular JS(TeamDropDownController和服务):

    <pre>
    app.controller('TeamDropDownController', 
    function ($scope, wingtipCrmService) {
    wingtipCrmService.getDropDownTeams().success(function (data) {
        $scope.teams = data.d.results;
    })
});
</pre>
<pre>
service.getDropDownTeams = function () {
          var restQueryUrl = "../_api/web/lists/getByTitle('Teams')/items/" +
                             "?$select=ID,Title";
          return $http({
              method: 'GET',
              url: restQueryUrl,
              headers: { "Accept": "application/json; odata=verbose" }
          })
      }
</pre>

我是开发sharepoint应用程序和Angular JS的新手。我一直试图找出这个问题的解决方案超过一个星期,在这个过程中我学到了很多东西。

我正在使用TeamDropDownController加载带有团队名称的select语句,然后使用playersnewcontroller添加新玩家记录。

添加播放器javascript一直有效,直到我添加代码以包含TeamName字段。我担心我使用控制器加载下拉列表的方式导致playersnewController出现问题。

2 个答案:

答案 0 :(得分:0)

错误消息:

  

“无法解析名为'{ListItemEntityTypeFullName}'的类型   模型。当模型可用时,每个类型名称必须解析为a   有效的类型。“。

通常在找不到具有指定名称的List Item实体时发生。在您的情况下,这意味着没有名称为SP.Data.PlayersListItem的实体。

以下端点允许正确确定列表项实体名称:

Uri: /_api/lists/getbytitle('{List Title}')?$select=ListItemEntityTypeFullName
Method: GET

以下jQuery示例演示了如何在创建列表项时动态确定实体类型名称:

function createListItem(webUrl,listTitle,itemProperties)
{
     var requestUri =  webUrl + "/_api/lists/getbytitle('" + listTitle + "')?$select=ListItemEntityTypeFullName";
     return executeJson({url:requestUri}).then(function(data){
          var requestUri =  webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items";
          itemProperties['__metadata'] = {'type': data.d.ListItemEntityTypeFullName};
          return executeJson({url: requestUri, method: 'POST', data: itemProperties}); 
     });
}


var properties = {'Title':'John Doe'};
createListItem(_spPageContextInfo.webAbsoluteUrl,'Contacts',properties)
.done(function(data)
{
    console.log('List item has been created');     
})
.fail(function(error){
    console.log(JSON.stringify(error));
});

,其中

function executeJson(options) 
{
    method = options.method || 'GET';
    headers = options.headers || {};
    headers["Accept"] = "application/json;odata=verbose";
    if(method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }      
    var ajaxOptions = 
    {       
       url: options.url,   
       type: method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if (typeof options.data != 'undefined') {
      ajaxOptions.data = JSON.stringify(options.data);
    }  
    return $.ajax(ajaxOptions);
}

答案 1 :(得分:0)

要解决我的问题,我添加了播放器记录而未填写TeamName查找字段。然后立即使用TeamName字段信息更新播放器记录。