Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mike Pitcher 6 posts 26 karma points
    Sep 13, 2011 @ 13:55
    Mike Pitcher
    0

    Using c# to create a RelatedLinks field

    I can retrieve the contents of a relatedlinks field by creating an XmlDocument from the returned value as below:

    XmlDocument links = new XmlDocument();
    links.LoadXml(part.GetProperty("products").Value);

    and then traversing the XmlDocument. However, I cannot use C# and the API to create the field in the first place.I am using code that looks like:

    // Create XmlDocument to hold products
    doc = new XmlDocument();
    
    // Insert declaration as first node
    XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0"null"yes");
    doc.AppendChild(declaration);
    
    // Create the root node
    rootElement = doc.CreateElement("links");
    doc.AppendChild(rootElement);
    
    // Look up the Product Id int productId = productsXref[thisProduct];                      // Add Product link to current Part linkElement = doc.CreateElement("link"); rootElement.AppendChild(linkElement);

    // Link Type is internal linkAttribute = doc.CreateAttribute("type"); linkAttribute.Value = "internal"; linkElement.SetAttributeNode(linkAttribute);
    // Link Title linkAttribute = doc.CreateAttribute("title"); linkAttribute.Value = thisProduct; linkElement.SetAttributeNode(linkAttribute);
    // Link Link linkAttribute = doc.CreateAttribute("link"); linkAttribute.Value = productId.ToString(); linkElement.SetAttributeNode(linkAttribute);
    // Link NewWindow linkAttribute = doc.CreateAttribute("newwindow"); linkAttribute.Value = "1"; linkElement.SetAttributeNode(linkAttribute); DocumentBeingWorkedOn.getProperty("relatedLink").Value = doc;

    The last statement above consistently fails with nul object exception.

    Can anyone help? I'm new to Umbraco.

  • Ehsan 8 posts 30 karma points
    Nov 28, 2014 @ 17:35
    Ehsan
    0

    hey dude did u figure this out yet??

    I am having the same problem.. I am not able to set/add value to my RelatedLinks field programatically! :s

  • Mike Pitcher 6 posts 26 karma points
    Nov 29, 2014 @ 13:33
    Mike Pitcher
    0

    Hi Ehsan

    Sorry but I am no longer using the version of Umbraco where I experienced this problem. I suggest you re-post your query because after three years I cannot find the code where I had the problem.

    Good luck!

     

    Mike

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies