vikbehal
vikbehal

Reputation: 1524

Reading files using C# from Sharepoint Assets Directory

How can we Read files using C# from Sharepoint Assets Directory.

like

string[] filePaths = Directory.GetFiles(@"D:\vikrant\Sneham\samplepics", "*.*", SearchOption.AllDirectories);

Here Instead of "D:\vikrant\Sneham\samplepics if I write location of asset library http://machinename:7/SiteAssets/SnehamBackgrounds/ then it is throwing error(URI related).

Requirement is to one by one read file names from asset libaray.

Upvotes: 0

Views: 2600

Answers (2)

Pabzt
Pabzt

Reputation: 506

Consider using EnsureSiteAssetsLibrary(). The function will return the SiteAssets-Library (SiteAssets will be created if they don't exist).

SPList assetsLibrary = SPContext.Current.Web.Lists.EnsureSiteAssetsLibrary();

Upvotes: 1

Andrew Adamich
Andrew Adamich

Reputation: 707

using (SPSite site = new SPSite("http://YourSite/"))
{
   using(SPWeb web = site.OpenWeb("/"))
   {
     SPList assetsLibrary = web.Lists["LibraryName"];

     foreach (SPListItem item in assetsLibrary.Items)
     {
        if(item != null)
        {
           Console.WriteLine(item.File.Name);
        }
     }
   }
}

Console.ReadKey();

Upvotes: 1

Related Questions