SharePoint - 项目属性中的参考文件

SharePoint - Reference file in item properties

(免责声明:这是我第一次与 SharePoint 打交道)

我正在尝试在 SharePoint 中添加文件并在列表项的 属性 中引用它。 属性 类型是 'Sparqube Lookup Classic'(我不知道那是什么,但快速搜索让我找到了这个:http://www.sparqube.com/SharePoint-Lookup-Column/)。

无论我尝试什么,我似乎都失败了。我在网上搜索过,但没有找到相关结果(可能搜索词有误?)。

这是我的半功能代码。当代码完成时,该项目在 (_x03a8__x03b7__x03c6__x03b9__x03) 属性 中没有附加文件。

    public void PublishDocToSP()
    {
        var clientContext = GetClient();
        SP.Client.File file;
        var folderName = "DocLib";

        // Upload file - Works OK.
        {
            var fileName = @"C:\Users\user\Desktop\file.pdf";
            var folder = clientContext.Web.Folders.GetByUrl(clientContext.Url + '/' + folderName);

            var info = new FileCreationInformation
            {
                ContentStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read),
                Overwrite = false,
                Url = clientContext.Url + '/' + folderName + '/' + Path.GetFileName(fileName),
            };
            file = folder.Files.Add(info);
            folder.Update();
            clientContext.ExecuteQuery();
        }

        // Add item in list and reference file in property. Not working!
        {
            var list = clientContext.Web.Lists.GetById(Guid.Parse("{6F15AACD-1132-4BD8-AC7D-36EA1A336D5C}"));

            var itemCreateInfo = new ListItemCreationInformation()
                                     {
                                         //FolderUrl
                                         //LeafName
                                         //UnderlyingObjectType
                                     };
            var li = list.AddItem(itemCreateInfo);
            li["DocmanId"] = 111;
            li["Title"] = "adfadfadfaf";
            li["Email_x0020__x039a__x03b1__x03c4"] = "trehagireue@mailcom"; // Email
            li["_x0391__x0394__x0391_"] = "ΑΗ-ΓΑ...";
            li["_x0391__x03c1__x03b9__x03b8__x03"] = "dfgdfg-sdf";
            li["_x03a8__x03b7__x03c6__x03b9__x03"] = new SP.SPFieldUrlValue(clientContext.Url + '/' + folderName + '/' + "file.pdf"){Description = "Test Desc"};
            li.Update();
            list.Update();
            clientContext.ExecuteQuery();
            var insertedId = li.Id;
        }
    }

知道我错过了什么吗?

更新:

检索现有列表项并查看字段数据我得到了这个:

var lv0 = item["_x03a8__x03b7__x03c6__x03b9__x03"] as Microsoft.SharePoint.Client.FieldLookupValue[];
{Microsoft.SharePoint.Client.FieldLookupValue[1]}
    [0]: {Microsoft.SharePoint.Client.FieldLookupValue}
lv0[0]
{Microsoft.SharePoint.Client.FieldLookupValue}
    base {Microsoft.SharePoint.Client.ClientValueObject}: {Microsoft.SharePoint.Client.FieldLookupValue}
    LookupId: 532
    LookupValue: "σσσ"
    TypeId: "{f1d34cc0-9b50-4a78-be78-d5facfcccfb7}"

现在,我想我必须找到获取这些数据的方法。 LookupId 似乎是文件 ID。我想知道如何从客户那里得到这个。我没有看到这样的 属性 被退回。

更新2:

终于,我成功地得到了上传的文件ID:。 但是当我在现场发送任何这些文件时,我收到错误或没有链接文件:

using SP = Microsoft.SharePoint;

string.Format("{0};#{1}", fileID, file.Name);
string.Format("{0};#{1:B}", fileID, listId); //listId is GUID
new Microsoft.SharePoint.Client.FieldLookupValue[] { new Microsoft.SharePoint.Client.FieldLookupValue { LookupId = fileID } };
new SP.SPFieldLookupValueCollection { new SP.SPFieldLookupValue(fileID, file.Name) };

我也尝试过不使用数组,只是简单的 FieldLookupValue。那也没用。 :(

好的。似乎 Sparqube Lookup Classic 使用文件标题并且无法处理没有标题的文件。所以,首先你必须设置标题,然后一切正常:

    public static void PublishDocToSP()
    {
        var clientContext = GetClient();
        SP.Client.File file;
        var folderName = "DocLib";

        // Upload file
        {
            var fileName = @"C:\Users\user\Desktop\file.pdf";
            var folder = clientContext.Web.Folders.GetByUrl(clientContext.Url + '/' + folderName);

            var info = new FileCreationInformation
            {
                ContentStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read),
                Overwrite = false,
                Url = clientContext.Url + '/' + folderName + '/' + Path.GetFileName(fileName),
            };
            file = folder.Files.Add(info);

            // !!! I set some value for Title field, because in my case, Title column is Display Column of Sparqube Lookup Classic
            ListItem itemFile = file.ListItemAllFields;
            itemFile["Title"] = Path.GetFileName(fileName);
            itemFile.Update();
            clientContext.Load(itemFile);

            clientContext.ExecuteQuery();
        }

        // Add item in list and reference file in property. Not working!
        {
            var list = clientContext.Web.Lists.GetById(Guid.Parse("{F682C057-9715-4F1C-BE1E-D451803FF389}"));
            var itemCreateInfo = new ListItemCreationInformation()
            {
                //FolderUrl
                //LeafName
                //UnderlyingObjectType
            };
            var li = list.AddItem(itemCreateInfo);
            li["Title"] = "adfadfadfaf";

            // Set value for Lookup Classic with single value selection
            li["sqLookupClassic"] = new SP.Client.FieldLookupValue()
            {
                LookupId = file.ListItemAllFields.Id
            };
            // !!! OR
            // li["sqLookupClassic"] = string.Format( "{0};#{1}", file.ListItemAllFields.Id, file.ListItemAllFields["Title"] );

            // !!! If 'Allow multiple values' option is selected for Lookup classic, you should set value in the following way:
            //li["sqLookupClassic"] = string.Format( "{0};#{1};#{2};#{3}", item1.Id, item1["Title"], item2.Id, item2["Title"] );

            li.Update();
            clientContext.ExecuteQuery();
            var insertedId = li.Id;
        }
    }