5

I have a template for a custom user profile that has a General Link field type. Here's what I am trying to do:

Sitecore.Security.Accounts.User.Profile.SetCustomProperty("ProfilePhoto", "http://placehold.it/350x150");
Sitecore.Security.Accounts.User.Profile.Save();

Problem is that by updating this way it messes with the General Link field for this field expects an XML.

Notice how this field is displayed (blank) after updating it the way above:

enter image description here

And then when I click on Insert Link it displays the yellow screen of death.

enter image description here

To fix that I have to click on Clear and then try to insert a link again.

Any idea?

6

First things first. The fact that you get YSOD on a malformed value is actually a bug. It is registered with Sitecore under public reference 96011.

That said; internally; General Link is an XML element. For your scenario to work, you need to store your value as such. The element looks like this:

<link
    text="Text"
    linktype="internal/external"
    url="url"
    anchor="anchor"
    title="title"
    class="class"
    querystring="querystring"
    target="target"
    id="id" />

So in your case, something like this should work:

<link linktype="external" url="http://placehold.it/350x150" />

I don't believe any of the other attributes will be required, for an external link such as yours.

Or the full code example:

Sitecore.Security.Accounts.User.Profile.SetCustomProperty("ProfilePhoto", "<link linktype=\"external\" url=\"http://placehold.it/350x150\" />");
Sitecore.Security.Accounts.User.Profile.Save();
| improve this answer | |
6

What Mark has outlined will work. The only downside is that you'll have to compose the XML yourself, and its structure could potentially change in future versions of Sitecore (however unlikely that is).

I've been trying to come up with a way of getting the XML string via Sitecore API. It seems that there is no straightforward way of doing that, but here's what I came up with:

public static class LinkHelper
{
    public static string GetLinkXml(Uri url, string linkType = "external")
    {
        string result;

        using (new SecurityDisabler())
        {
            Database db = Database.GetDatabase("core");

            // Any real link field will do for our purposes.
            Item item = db.GetItem("/sitecore/masters/Application");
            var linkField = new LinkField(item.Fields["__Help link"]);

            item.Editing.BeginEdit();

            linkField.Url = url.ToString();

            linkField.LinkType = linkType;

            // Get the final XML.
            result = linkField.Xml.OuterXml;

            // Cancel the changes so that they aren't saved to the database.
            item.Editing.CancelEdit();
        }

        return result;
    }
}

If performance of generating such links isn't an issue for you, this seems usable (albeit not tested in real projects).

Here's a usage example:

string linkXml = LinkHelper.GetLinkXml(new Uri("http://placehold.it/350x150"))
profile.SetCustomProperty("ProfilePhoto", linkXml);
profile.Save();
| improve this answer | |
0

This question was the top result when searching for how to edit a General Link field programatically almost 3 years later. I didn't like either fiddling with XML or getting an existing link from the Core database, but Dmytro's answer did put me on the right track. Maybe this doesn't work for User.Profile.SetCustomProperty() like the OP but this is what you can do with a regular item:

item.Editing.BeginEdit();
item.Fields["General link field name"].Value = string.Empty; // empty any existing value
new LinkField(item.Fields["General link field name"])
{
    Url = "https://www.your.url",
    LinkType = "external"
};
item.Editing.EndEdit();

Notice I'm not assigning the new LinkField to anything, not even back to the original item, the LinkField has an internal reference to the raw field and updates it with the proper xml as you set properties like Url and LinkType.

| improve this answer | |

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.