0

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);
newWeb.Update();
if (item.Attachments.Count > 0)
{
    string attachUrl = SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, "/" + item.Attachments[0]);
    string fileName=StoreAttachmentToPicLib(attachUrl,properties.Web,newWeb);

    item.Attachments.Delete(fileName);
    item.Update();
}
else
{
    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);
    library.Update();
    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?

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

2 Answers 2

0

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;
  }
  else
  {
    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.

http://blog.mastykarz.nl/inconvenient-spwebgetfilestring/

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

EDIT

just noticed what your trying todo!

you should do this instead!

https://stackoverflow.com/questions/19125529/office-365-sharepoint-upload-files-to-documents-library

what you are trying todo is this:

http://msdn.microsoft.com/en-us/library/ms477413.aspx

which wont work.

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

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);
list.Update();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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