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

时间:2015-04-29 13:51:42

标签: c# sharepoint

(免责声明:这是我第一次处理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。我想知道如何从客户那里得到这个。我没有看到这些财产被退回。

UPDATE2:

最后,我设法获得了上传的文件ID:https://stackoverflow.com/a/22254339/2173353。 但是当我发送任何字段时,我得到一个错误或没有任何链接文件:

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。这也没有用。 :(

1 个答案:

答案 0 :(得分:1)

行。似乎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;
        }
    }