0

how to set image which is in images folder of the project to newPage.ListItem[FieldId.PublishingPageImage] = image in the folder;

Will the image get loaded to provisioned site when a site is provisioned?

5
  • Should I upload a image to sitecollectionimages folder and reference the url for publishingpageimage?
    – Roxanne
    May 15, 2011 at 21:43
  • Does the down voter want to give a reason? May 16, 2011 at 2:30
  • @AbeMiessler It was probably downvoted because it's a low quality question - generally poorly worded and formatted. Perhaps someone reached edit fatigue?
    – Alex Angas
    May 18, 2011 at 0:42
  • @Alex: Thanks for pointing it out. My writing skills are not as sophisticated as it should be. I tend to leave out words. I have no idea why.
    – Roxanne
    May 19, 2011 at 18:36
  • It's not always easy! Just remember that the more effort you put into your question to make it clear, written well, and formatted properly, the more effort people will want to put into answering it. This has been proven! :)
    – Alex Angas
    May 19, 2011 at 20:47

2 Answers 2

1

You'll need to have the image already uploaded somewhere on your site. You can do this manually or if you have the image in a VS project, you can deploy it with a module.

Then set the URL for your publishing image field

ImageFieldValue pageImage = newPage.ListItem[FieldId.PublishingPageImage] as ImageFieldValue;

if (null == pageImage)
    pageImage = new ImageFieldValue();

pageImage.ImageUrl = "http://yoursite/yourimage.jpg";
newPage.ListItem[FieldId.PublishingPageImage] = pageImage;
newPage.Update();
4
  • Your solution looks like as I could never get the module to work. Let me try and I will let you how it went. Thanks
    – Roxanne
    May 15, 2011 at 22:59
  • {"Object reference not set to an instance of an object."} getting this error on this line pageImage.ImageUrl = "yoursite/yourimage.jpg";
    – Roxanne
    May 16, 2011 at 1:56
  • btw, Image has been uploaded to site collection.
    – Roxanne
    May 16, 2011 at 2:14
  • You may need to check if no image had ever been set, in which case you would get a null reference exception. See the edit above. May 16, 2011 at 2:51
0

If you add SharePoint "Images" Mapped Folder to your project and add your image to that mapped folder, Visual Studio will automatically deploy it with your wsp. The url to a file in the images mapped folder is "_layouts/images/your-image.jpg"

If you want an image deployed to another location (not TEMPLATE\IMAGES), you can create a module and add your image to that module. This will cause Visual Studio to add a from/to entry to the module's element manifest. For example if you wanted the image to go into the Style Library of your site, it would look like this:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="your module name" Url="Style Library" RootWebOnly="FALSE">
    <File Path="path-in-your-VS-project\your-image.jpg" Url="your-image.jpg" Type="GhostableInLibrary" />
  </Module>
</Elements>
3
  • Thanks for posting the answer but I could never get the module to work. I even called MS support and even they could get it work with live meeting where they could see all my code.
    – Roxanne
    May 15, 2011 at 22:57
  • The 2 things to check in the elements.xml for you module are (1) get the path right, and (2) make sure you add the GhostableInLibrary, which Visual Studio does not automatically add for you. The from-path will be the Path in the Module node appended by the Path in the File node. The to-path will be Url in the Module node appended by the Url in the File node. ex: <Module Url="Style Library/Images/CompanyName" Path="My Images" RootWebOnly="TRUE"> <File Path="myImage.jpg" Url="MyImage.jpg" Type="GhostableInLibrary"/> will copy: My Images/myImage.jpg => Style Library/Images/CompanyName/myImage.jpg
    – Laurie
    May 15, 2011 at 23:10
  • If you have a sample project and you know for sure that it works then I would love to get a copy of it.
    – Roxanne
    May 16, 2011 at 1:55

Your Answer

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

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