简体   繁体   English

SharePoint联机加载项中的“标题”字段初始化错误

[英]'Title' field initialization error in SharePoint online add-in

I'm trying to write a SharePoint add-in which has a part that has to assign the name of a selected file in "Documents" list to a string called title. 我正在尝试编写一个SharePoint加载项,该加载项的一部分必须将“文档”列表中选定文件的名称分配给名为title的字符串。

    protected void Page_Load(object sender, EventArgs e)
    {

        var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

        int listItemID;

        listItemID = GetListItemIDFromQueryParameter();

        using (var clientContext = spContext.CreateUserClientContextForSPHost())
        {
            clientContext.Load(clientContext.Web,
            web => web.Title,
            web => web.CurrentUser,
            web => web.Lists);

            List doclist = clientContext.Web.Lists.GetByTitle("Documents");
            Microsoft.SharePoint.Client.ListItem item = doclist.GetItemById(listItemID);

            string title = item.File.Title;
        }
    }

Function GetListItemIDFromQueryParametr(); 函数GetListItemIDFromQueryParametr(); works like intended - it returns an ID int value of the selected file. 像预期的那样工作-它返回所选文件的ID int值。 I want to get the file's name by that ID. 我想通过该ID获取文件名。 The following code returns an error - 以下代码返回错误-

Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException: 'The property or field 'Title' has not been initialized. Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException:'属性或字段'Title'尚未初始化。 It has not been requested or the request has not been executed. 尚未请求或尚未执行请求。 It may need to be explicitly requested.' 可能需要明确要求它。

I am now trying to figure out how to initialize that field, but so far I haven't found anything that would help me. 我现在正试图弄清楚如何初始化该字段,但是到目前为止,我还没有找到任何可以帮助我的东西。 Any clues would be much appreciated. 任何线索将不胜感激。

You haven't loaded your listitem yet. 您尚未加载列表项。 Before you can read properties in csom you need to load them, like how you have loaded your web. 您必须先加载csom中的属性,然后才能加载它们,例如加载网络的方式。 You also need to execute the queries you load which you haven't done. 您还需要执行尚未加载的查询。

Add a clientContext.ExecuteQuery(); 添加一个clientContext.ExecuteQuery(); after the line where you initialise do list 在您初始化的do list

Apologies for poor formatting, I'm on a mobile device 抱歉,格式不正确,我在移动设备上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM