Tuesday, November 19, 2013

SharePoint Hyperlink Fields

I seem to always forget how to access, set, and update a SharePoint 2010 Hyperlink field. I use these frequently to link items from one list to another. So here is my generic but tried and true code for these fields:

PowerShell
# Make sure the SharePoint namespace is loaded
Add-pssnapin Microsoft.SharePoint.PowerShell –erroraction silentlycontinue;

# Set some sample variables
$yourservername = "contoso.local";
$yoursitepath = "sites/Accounting/Audits";
$yourlistname = "Links";
$youritemid = 18;
$yourhyperlinkcolname = "RelatedCase";


# Steps to get the Url and Text from an existing list item
# 1 – Get a reference to the listitem
$web = get-spweb ("http://{0}/{1}" –f $yourservername, $yoursitepath);
$list = $web.lists.trygetlist($yourlistpath);
$item = $list.getitembyid($youritemid);

# 2 – Create an SPFieldUrlValue object from the listitem column
$itemHyperLink = new-object Microsoft.SharePoint.SPFieldUrlValue($item[$yourhyperlinkcolname].ToString());

# 3 - Display the text and url
Write-host –f green "Text: " –nonewline;
Write-host –f yellow $itemHyperLink.Description;
Write-host –f green "Url : " –nonewline;
Write-host –f yellow $itemHyperLink.Url;

# Steps to create a new item with a hyperlink
# 1 – Similar to #1 above get reference to a list
$web = get-spweb ("http://{0}/{1}" –f $yourservername, $yoursitepath);
$list = $web.lists.trygetlist($yourlistpath);

# 2 – Create an empty SPFieldUrlValue
$HyperLinkField = $list.fields[$yourhyperlinkcolname] -as [Microsoft.SharePoint.SPFieldUrl];

# 3 – Create an empty SPFieldUrlValue
$newHyperLink = new-object Microsoft.SharePoint.SPFieldUrlValue;

# 4 – Populate the description (text) and Url as desired
$newHyperLink.Description = "Google";
$newHyperLink.Url = "http://google.com";

# 5 – Create a new list item
$newItem = $list.items.add();

# 6 – Set the field using parse and set value method
#     Note: You would also have to set any other required fields
$HyperLinkField.parseandsetvalue($newItem,$HyperLinkField.tostring());

# 7 – Save the update
$newItem.update();
$list.update();

# Dispose objects
$web.dispose();


C# (Console Application)
// <>Partial namespace listing
using Microsoft.SharePoint;

// Set some sample variables
string yourservername = "contoso.local";
string yoursitepath = "sites/Accounting/Audits";
string yourlistname = "Links";
int youritemid = 18;
string yourhyperlinkcolname = "RelatedCase";

// <>Code omitted, insert code into Main

// Access the site object
using (SPSite site = new SPSite(string.Format("http://{0}/{1}", yourservername, yoursitepath)))
{

    // Access the web object
    using (SPWeb web = site.OpenWeb())
    {

        // Steps to get the Url and Text from an existing list item
        // 1 – Get a reference to the listitem
        SPList list = web.Lists.TryGetList(yourlistname);
        SPListItem item = list.GetItemById(youritemid);

        // 2 – Create an empty SPFieldUrlValue
        SPFieldUrlValue itemHyperLink =
            new SPFieldUrlValue(item[yourhyperlinkcolname].ToString());

        // 3 - Display the text and url
        Console.WriteLine(string.Format("Text: {0}",
            itemHyperLink.Description));
        Console.WriteLine(string.Format("Url : {0}",
            itemHyperLink.Url));

        // Steps to create a new item with a hyperlink
        // 1 – Create an empty SPFieldUrlValue
        SPFieldUrlValue  newHyperLink = new SPFieldUrlValue();

        // 2 – Populate the description (text) and Url as desired
        newHyperLink.Description = "Google";
        newHyperLink.Url = "http://google.com";

        // 3 – Create a new list item
        SPListItem newItem = list.Items.Add();

        // 4 – Set the column to the SPFieldUrlValue
        //     Note: You would also have to set any other required fields
        newItem[yourhyperlinkcolname] = newHyperlink;

        // 5 – Save the update
        newItem.Update();


   } // end using web

} // end using site

// Let the user terminate the program
Console.Write("Press return to quit... ");
Console.ReadLine();


1 comment:

  1. This article is really a pleasant one it helps new net people, who are wishing for Share point. I am glad that I had the fortune to stumble across your blog. I like it!

    Top 9 Dental Websites Designed by Optimized360.Com

    ReplyDelete