I am creating new web in the ItemAdded event receiver and I want to upload the picture that is attached with the item to the newly created site's picture library and if no attachment is there then I want to upload the Sharepoint's blank image file which has the path. _layouts/15/images/256_icgen.png

My code is:

newWeb = properties.Web.Webs.Add(url, url, desc, 1033, Webs.Community.WebTemplate, true, false);
newWeb.Lists.Add("CommunityPicture", string.Empty, SPListTemplateType.PictureLibrary);
if (item.Attachments.Count > 0)
    string attachUrl = SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, "/" + item.Attachments[0]);
    string fileName=StoreAttachmentToPicLib(attachUrl,properties.Web,newWeb);

    string attachUrl = "_layouts/15/images/256_icgen.png";                        
    string fileName = StoreAttachmentToPicLib(attachUrl, properties.Web, newWeb);
private string StoreAttachmentToPicLib(string attachUrl,SPWeb ListWeb,SPWeb newWeb)
    //List web is All Communities web which will refer to communities list and the newWeb is the subsite of community.
    SPFile oFile = ListWeb.GetFile(attachUrl);
    string fileName = oFile.Name;
    byte[] binFile = oFile.OpenBinary(); // error comes here "Cannot open file"

    SPFolder library = newWeb.Folders["CommunityPicture"];
    library.Files.Add("HomePic.jpg", binFile, true);
    return fileName;

My problem is that I the file oFile is found successfully but when the control reaches to OpenBinary() error is thrown.

Am I going through right way or what am I missing in my code?

  • can you post the error here? sorry just noticed the error ;) – Ali Jafer Dec 12 '13 at 9:11
  • I am debugging and let you know – users1100 Dec 12 '13 at 9:19
  • Cannot open file "_layouts/15/images/256_icgen.png". – users1100 Dec 12 '13 at 9:23

Use this method to get the file!

public static SPFile GetFileObject(this SPWeb web, string url)
  object o = web.GetFileOrFolderObject(url);
  if (o is SPFile)
    return (SPFile)o;
    throw new FileNotFoundException();

Call the method above like so:

SPFile oFile = GetFileObject(ListWeb,attachUrl);

It shouldn't break then as it's getting the file properly! Something to do with how you build the url together, either it will show false or true.


You can whilst debugging hover over oFile and see its values and if they are correct.


just noticed what your trying todo!

you should do this instead!


what you are trying todo is this:


which wont work.

  • should url be full url or relative? – users1100 Dec 12 '13 at 9:24
  • GetFileOrFolderObject is relative, but the site can be the site collection url. – Ali Jafer Dec 12 '13 at 9:30
  • I am getting oFile successfully but the error comes when I call OpenBinary() method – users1100 Dec 12 '13 at 9:34
  • ok im presuming that the account that your using has permission to open the file? – Ali Jafer Dec 12 '13 at 9:36
  • yes sir, I manually changed the permission of that file in the 15 hive – users1100 Dec 12 '13 at 9:58

Here is my code that works that runs in a feature event receiver

SPWeb web = properties.Feature.Parent as SPWeb;

//Get the image from site collection root style library
SPWeb fromWeb = web.Site.RootWeb;            
SPFile imgFile = fromWeb.GetFile("Style Library/Images/logo.png");

//convert to binary
byte[] picFile = imgFile.OpenBinary();
string fileName = imgFile.Name;

SPList list = web.Lists["Media"];
SPFolder picLibrary = list.RootFolder;

//Add to the right folder in current site
picLibrary.Files.Add(fileName, picFile, true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.