如何通过Silverlight客户端对象模型检索列表项的附件,或至少从SharePoint检索附件的路径?

时间:2011-06-29 18:28:44

标签: c# silverlight-4.0 sharepoint-2010

我有一个SharePoint网站,我正在使用Silverlight客户端对象模型设置Silverlight前端。我需要提取数据的其中一个列表有附件。我需要找到一种列出这些附件的方法,但我似乎无法找到一种方法。

ListItem上有一个“附件”字段,但它只是一个布尔表明附件是否存在。

我已经看过很多使用SPListItem的例子,但是我怎样才能使用Silverlight客户端对象模型呢?

2 个答案:

答案 0 :(得分:6)

我也遇到了这个问题,在ScottyG30的回答和this thread回答的帮助下,我编写了一个从ListItem中检索附件的函数:

// this method needs to be executed in background thread
public String[] GetAttachments(ClientContext ctx, List list, ListItem item)
{
    // these properties can be loaded in advance, outside of this method
    ctx.Load(list, l => l.RootFolder.ServerRelativeUrl);
    ctx.Load(ctx.Site, s=>s.Url);
    ctx.ExecuteQuery();

    // get the item's attachments folder 
    Folder attFolder = ctx.Web.GetFolderByServerRelativeUrl( list.RootFolder.ServerRelativeUrl + "/Attachments/" + item.Id);
    FileCollection files = attFolder.Files;
    // I needed only urls, so I am loading just them
    ctx.Load(files, fs => fs.Include(f => f.ServerRelativeUrl));
    ctx.ExecuteQuery();

    // now you have collection of files
    return (from file in files select ctx.Site.Url + file.ServerRelativeUrl).ToArray();
}

虽然这对我有用,但对于大型列表中的所有项目(每个项目都是executing query)需要附件(网址)时,我认为这不是最佳解决方案。

答案 1 :(得分:0)

ClientContext spContext = ClientContext.Current;
File.OpenBinaryDirect(spContext, spContext.Web.ServerRelativeUrl + "/lists/[ListName]/Attachments/[ItemID]/[File Name]", (w, f) =>
        {
            var foo = f.Stream;

        }, (q, w) => { 
            handler(this, new Exception(w.Message)); 
        });