开发者

SP 2010 Attach File Client Object Model

I have created some code to create a list item programmatically. Now I would like to attach a file to the list. I'm getting a 401 error: unauthorized. I have set the credentials which works for creating the item but not attaching the file. Any ideas?

Dim credentials As New System.Net.NetworkCredential(MyUser, MyPassword, MyDomain)

Dim SiteUrl As String = MyUrl
Dim clientContext As New ClientContext(SiteUrl)

clientContext.Credentials = credentials

Dim list As List = clientContext.Web.Lists.GetByTitle(MyList)
Dim itemCreateInfo As New ListItemCreationInformation()
Dim listItem As Microsoft.SharePoint.Client.ListItem = list.AddItem(itemCreateInfo)

listItem("Title") = "Test Programmatically Create Item"
listItem("Subject") = "TEST"
listItem("Class") = "101"
listItem("Section开发者_JAVA百科") = "20"
listItem.Update()

listItem.Update()
clientContext.ExecuteQuery()

Dim fStream As Stream = File1.PostedFile.InputStream                
Dim attachmentPath As String = String.Format("/{0}/Lists/{1}/Attachments/{2}/{3}", MySite, MyList, listItem.Id, MyFileName)                

'-- This Line Fails with the following error
'-- The remote server returned an error: (401) Unauthorized.               
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, attachmentPath, fStream, True)

I know setting the credentials is correct because if I don't set them then I get this error trying to create the list item.

Any thoughts are greatly appreciated or if there is a better way to attach a file to a list item please let me know.

Thanks!


It worked for me. I first read the file that needs to be uploaded into the memory and used MemoryStream to pass the file to SaveBinaryDirect.

    static void  moveFileFromOneFarmToAnother()
   {

        string srcSiteUrl = "SourceSiteUrl";
        string srcFileRelUrl = "ReletiveUrlOfTheListItemAttachment";
        ClientContext srcCC = new ClientContext(srcSiteUrl);
        Web srcWb = srcCC.Web;
        srcCC.Load(srcWb, w => w.ServerRelativeUrl);
        srcCC.ExecuteQuery();
        FileInformation fI = Microsoft.SharePoint.Client.File.OpenBinaryDirect(srcCC, srcWb.ServerRelativeUrl + srcFileRelUrl);
        byte[] buffer = new byte[16 * 1024];
        byte[] byteArr;
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = fI.Stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            byteArr = ms.ToArray();
        }
        MemoryStream mnm = new MemoryStream(byteArr);
        srcCC.ExecuteQuery();
        string desSiteUrl = "DestinationSiteUrl";
        string desFileUrl = "ReletiveUrlOfTheListItemAttachment";
        ClientContext cc = new ClientContext(desSiteUrl);
        Web wb = cc.Web;
        cc.Load(wb, w1 => w1.ServerRelativeUrl);
        cc.ExecuteQuery();
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(cc, wb.ServerRelativeUrl + desFileUrl, mnm, true);
        cc.ExecuteQuery();
   }


Take a look at the following link. This should point you in the right direction.

http://www.sharepointdevwiki.com/display/public/Creating+a+List+Item+instance+programmatically+using+the+object+model?focusedCommentId=7897226

You could also take a look at SPSecurity.RunWithElevatedPrivileges().

Good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜