C# SharePoint-项目属性中的引用文件
(免责声明:这是我第一次处理SharePoint) 我正在尝试在SharePoint中添加文件,并在列表项的属性中引用该文件。属性类型是“Sparqube Lookup Classic”(我不知道那是什么,但快速搜索使我找到了这个:) 无论我尝试什么,我似乎都失败了。我在网上搜索过,但没有找到相关的结果(可能是错误的搜索词?) 这是我的半功能代码。代码完成时,该项在(C# SharePoint-项目属性中的引用文件,c#,sharepoint,C#,Sharepoint,(免责声明:这是我第一次处理SharePoint) 我正在尝试在SharePoint中添加文件,并在列表项的属性中引用该文件。属性类型是“Sparqube Lookup Classic”(我不知道那是什么,但快速搜索使我找到了这个:) 无论我尝试什么,我似乎都失败了。我在网上搜索过,但没有找到相关的结果(可能是错误的搜索词?) 这是我的半功能代码。代码完成时,该项在(\u x03a8\uuuuux03b7\uuuuux03c6\uuuuuuuux03b9\uuuuux03)属性中没有附加任何文件
\u x03a8\uuuuux03b7\uuuuux03c6\uuuuuuuux03b9\uuuuux03
)属性中没有附加任何文件
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;
}
}
最好尝试发布专门针对SharePoint相关问题的帖子,这样您可能会更容易看到SharePoint专家