SPFile in Sharepoint API

In Sharepoint API SPFile class procides us for working with Sharepoint list attachments. I want to explain SPFile class with some application. first console application working with MyTestListKK List attachments and display Attachment name and their file size;

using (SPSite objSite = new SPSite(“http://testsiteKK”))

{

using (SPWeb objWeb = objSite.OpenWeb())

{

SPList objList = objWeb.GetList(“/Lists/MyTestListKK/”);

foreach(SPListItem item in objList.Items)

{

foreach (string name in item.Attachments)

{

string fullname = item.Attachments.UrlPrefix + name;

SPFile files = item.Web.GetFile(fullname);

Console.WriteLine(“File Name {0}, fileSize {1} kb”,

fullname, files.Length);

}

}

}

}

}

This console application connect to http://testsiteKK/ (you can change it your test enviromet) and get your own list.

Download A List Attachments Programatically

foreach (string name in item.Attachments)

{

string fullname = item.Attachments.UrlPrefix + name;

SPFile files = item.Web.GetFile(fullname);

Console.WriteLine(“Downloded file: {0} “, name);

byte[] binFile = files.OpenBinary();

System.IO.FileStream fstream =

System.IO.File.Create(“c:\\Temp\\” + files.Name);

fstream.Write(binFile, 0, binFile.Length);

}

About Krishana Kumar

Krishana Kumar is SharePoint Architect/Trainer having Architecture experience with high volumes at Enterprise level and global scale - creation of highly scalable solutions with global user base and geographically distributed architectural components. Good knowledge of SharePoint best practices and governance models. I hold Two Master degree in Computer Science with over 11 years of experience working on Microsoft Technologies specially SharePoint, Project, .NET and other Information Worker Technologies. Having good exposer in Client side scripting Angular.js, backbone and Node. I am currently responsible for SharePoint Infrastructure set up and leading teams in various medium and large scale projects, architecting, designing & installing SharePoint farms, developing custom components,, and providing advanced SharePoint administration and development training to teams and customers. I regularly speaks in various SharePoint User Groups and other Events. I have MCSA Windows Azure, MCSA Office 365, MCSE & MCSD SharePoint 2013, Microsoft Certified Developer (MCD) and holds MCPD, MCTIP and MCTS for SharePoint 2010, MCTS MOSS 2007 & WSS 3.0, MCPD, MCITP (EPM 2010 & 2007) and MCSD .NET.
This entry was posted in General Interest. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *