0

I have use var url=oListItem["PublishingRollupImage"];

but it contain Img tag & I want only Image url.

I also use below code but it is not available in CSOM:

 ImageFieldValue imageUrl = oListItem["PublishingRollupImage"] as ImageFieldValue;
                if (imageUrl != null)
                {
                    item.imageUrl = imageUrl.ImageUrl;
                    item.imageWidth = imageUrl.Width.ToString();
                    item.imageHeight = imageUrl.Height.ToString();
                }
2
  • It gives Error:(ImageFieldValue)oListItem["PublishingRollupImage"] Cannot cast 'oListItem["PublishingRollupImage"]' (which has an actual type of 'string') to 'Microsoft.SharePoint.Publishing.Fields.ImageFieldValue' Jul 26, 2017 at 6:09
  • I understand now, you want to do it in CSOM! I tried to clarify the question for you. From what I know, the ImageFieldValue is not exposed in CSOM so you would have to use the first approach and then do som regex or string parsing Jul 26, 2017 at 6:11

1 Answer 1

1

PublishingRollupImage gives string value containing <img .../> tag.

So, as Robert mentioned in comments, you need to use Regex to extract the src value from the string.

You can use the sample code to extract the src value from the field.It will give the server relative url of the image.

ListItem targetListItem = list.GetItemById(8);                

context.Load(targetListItem, item => item["PublishingRollupImage"]);
context.ExecuteQuery();

//gives value containing img tag
var imageField = Convert.ToString(targetListItem["PublishingRollupImage"]);

//Regex to extract the src value from the img tag
string imageUrl = Regex.Match(imageField, "<img.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase).Groups[1].Value;

Below is the end result I got in a sample console application.

enter image description here

Regex reference - Regular Expression to get the SRC of images in C#

You need to add System.Text.RegularExpressions dll to your code to use regex.

0

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.