0

I would like to add a quick link to my SharePoint Foundation 2010 site through code (like in a custom web part). If I do it through the UI, I can successfully add a quick link that points to whatever I want (e.g. www.google.com) without any issues. I go to the page, click on the link, and it takes me to Google, no problems. If I add the same quick link through code (don't ask why I need to do it like that, just assume I do), I get the following error when I navigate to the page:

<nativehr>0x81070215</nativehr><nativestack></nativestack>Cannot open
"/clients/www.google.com": no such file or folder.

Here is the code I'm using to create the navigation item:

SPWeb masterweb = SPContext.Current.Site.OpenWeb("clients");
masterweb.AllowUnsafeUpdates = true;
string url = "www.google.com";
string title = "Google";
SPNavigationNodeCollection quickLaunch = masterweb.Navigation.QuickLaunch;
SPNavigationNode newnode = new SPNavigationNode(title, url, false);
masterweb.Navigation.QuickLaunch.AddAsLast(newnode);
masterweb.AllowUnsafeUpdates = false;

Is there a different way to add a Quick Launch link that I'm missing, or is there a property I'm overlooking or....?

Back Story: I have a site that contains a library of identical subsites. On the main site, I have pages that contain a library of custom web parts that pull information based on the site that has been selected (through the QueryString value). In launching those pages, I need links on the Quick Link bar that point to those pages with the Query String in tact. So, I want to basically build the Quick Link bar on the fly. So that, whatever site name is selected in the Query String, I need to append that back to the URL specified for the Quick Link. The purpose of all of this is to create a central "portal page" that can be altered at any time rather than have a million iterations of the same thing for each individual subsite (of which there are around 280).

0

Of course, RIGHT after I posted this question, I figured it out.

1) First off, the third argument in the SPNavigationNode constructor is intended to specify whether or not the link in question is external. In this case, www.google.com is external, so this needs to be set to "true."

SPNavigationNode newnode = new SPNavigationNode(title, url, true);

2) Second, if the link is external, it needs to have the http:// header. Otherwise, it will try to send you to [currentsite]/www.google.com

string url = "http://www.google.com";

There are other problems with this code. For instance, this will add a new Quick Link into the Quick Link registry, but will not remove old links. So, for my instance, it will just keep adding links onto the Navigation bar. So, rather than add a link, I need to update the existing link. Even so, this code only runs after all the Quick Launch stuff has been rendered unto the page. So, the changes will only go into effect AFTER you refresh... which is not viable. So, I still have work to do on this task for my purpose, but I believe the above solution should answer the question for those who experience this issue in the future....... and don't have the luxury of just throwing SharePoint straight into the garbage where it belongs.

UPDATE In the end, I went with a JavaScript option in SP Designer that will just update the href property of the link to what I want it to be. It's something else to maintain, but that just seemed like the cleanest option to me:

    $(document).ready(function(){           
        var QueryString = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&')
        $("span:contains('My Link')").closest("a").attr("href", "MyPage.aspx?" + QueryString);      
    });

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.