How to retrieve attachment from Office 365 SharePoint C#

This article shows you the code that I tried for 2-3 days to get it working, to retrieve attachment from Office 365 SharePoint.

I found piece and pieces from the Internet and put them together, finally it is working for me.

Follow the code and change to your credential it will work.

using System;

using System.IO;

using System.Linq;

using System.Net;

using System.Security;

using Microsoft.SharePoint.Client;

using SP = Microsoft.SharePoint.Client;


namespace Microsoft.SDK.SharePointServices.Samples


    class RetrieveListItemsInclude


        static void Main()


            String listName = “NewList”;


            //string siteUrl = “http://SharePoint/MySite”;

            string siteUrl =”;


            ClientContext clientContext = new ClientContext(siteUrl);


            string password = “Password”;

            SecureString securePassword = new SecureString();


            foreach (char c in password)




            clientContext.Credentials = new SharePointOnlineCredentials(“”, securePassword);


            Site oSite = clientContext.Site;




            // Get the Web

            Web oWeb = clientContext.Web;




            SP.List oList = clientContext.Web.Lists.GetByTitle(“NewList”);


            CamlQuery camlQuery = new CamlQuery();

            camlQuery.ViewXml = “”;


            ListItemCollection collListItem = oList.GetItems(camlQuery);



            clientContext.Load(collListItem, items => items.Include(

        item => item[“Title”],

        item => item[“FileRef”],

        item => item.AttachmentFiles,

        item => item.Id,

        item => item.File,

        item => item.File.ServerRelativeUrl





            foreach (ListItem oListItem in collListItem)





                    int intAttachCount = oListItem.AttachmentFiles.Count;


                    AttachmentCollection AttachCol = oListItem.AttachmentFiles


                    if (intAttachCount > 0)


                        Attachment att = AttachCol.First();


                        String filename = att.FileName;


                    string strTest = oSite.Url + “/Dev%20Site/” +

                          “/Lists/” + listName + “/Attachments/” +



                        string strfile = oSite.Url + “/Dev%20Site/” +

                          “/Lists/” + listName + “/Attachments/” +

                          oListItem.Id + “/” + filename;


                        var fileRef = att.ServerRelativeUrl;

                        var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);



                        using (FileStream file = new FileStream(filename, FileMode.Create))












About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress ( till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Community, Microsoft Office and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s