SharePoint 2010: Create a bookmark button that adds a page to your My Links

5.4k views Asked by At

I am trying to create a link/button on my masterpage which when clicked, adds the current page to the user's My Links list. This is merely a shortcut to save the user from having to navigate to their My Site and add the link manually.

[This blog post] gives a solution to this problem, but I get a JavaScript error on the second line of the "Add Link" dialog (QuickLinksDialog2.aspx) because the frameElement property is null:

<script language="Javascript">
    var form = document.forms[0];
    var args = window.parent.frameElement.dialogArgs;

Regardless, Portal.js appears to contain all the functions that the My Links page (_layouts/MyQuickLinks.aspx) uses to add links to this list.

Can anyone suggest how I might go about calling one/some of these functions from my masterpage so that the "Add Link" dialog is opened with the title and URL fields pre-poulated?

2

There are 2 answers

0
AudioBubble On BEST ANSWER

I ended up using the object model to create the My Links (as apposed to the popup dialog).

The upside to this is that adding a link is now only a 1-click process, the downside is that the user does not have the opportunity to rename the link or assign it to a group (personally, I've hidden the groups from the UI anyway as we didnt need them so this was a non-issue for me).

For those interested, I created a little usercontrol which just houses an ajaxified button which you can drop onto your masterpage / page layout. My code for this is as follows:

HTML


<script type="text/javascript">
    function FavouriteImageButton_AddMyLink_Clicked() {
        SP.UI.Notify.addNotification("Bookmark generated successfully.");
    }

    function FavouriteImageButton_RemoveMyLink_Clicked() {
        SP.UI.Notify.addNotification("Bookmark deleted successfully.");
    }
</script>

<asp:UpdatePanel ID="UpdatePanel" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:ImageButton ID="FavouriteImageButon" runat="server" OnCommand="FavouriteImageButton_Command" />
    </ContentTemplate>
</asp:UpdatePanel>

C#


private struct FavouriteButtonCommandNames
{
    public const string AddMyLink = "AddMyLink";
    public const string RemoveMyLink = "RemoveMyLink";
}

protected void Page_PreRender(object sender, EventArgs e)
{
    // Initialise the favourites button according to whether or not the page already exists in the My Links list.
    this.FavouriteImageButon.ImageUrl = "/_layouts/images/favourite_add.png";
    this.FavouriteImageButon.AlternateText = "Add to My Links";
    this.FavouriteImageButon.CommandName = FavouriteButtonCommandNames.AddMyLink;
    this.FavouriteImageButon.CommandArgument = null;

    UserProfileManager userProfileManager = new UserProfileManager(SPServiceContext.Current);
    UserProfile currentUser = userProfileManager.GetUserProfile(false);

    foreach (QuickLink quickLink in currentUser.QuickLinks.GetItems())
    {
        if (quickLink.Url.ToLower() == this.Page.Request.Url.ToString().ToLower())
        {
            this.FavouriteImageButon.ImageUrl = "/_layouts/images/favourite_delete.png";
            this.FavouriteImageButon.AlternateText = "Remove from My Links";
            this.FavouriteImageButon.CommandName = FavouriteButtonCommandNames.RemoveMyLink;
            this.FavouriteImageButon.CommandArgument = quickLink.ID.ToString();
            break;
        }
    }
}

protected void FavouriteImageButton_Command(object sender, CommandEventArgs e)
{
    UserProfileManager userProfileManager = new UserProfileManager(SPServiceContext.Current);
    UserProfile currentUser = userProfileManager.GetUserProfile(false);

    switch (e.CommandName)
    {
        case FavouriteButtonCommandNames.AddMyLink:
            // Create the link.
            currentUser.QuickLinks.Create(
                SPContext.Current.File.Title, 
                this.Page.Request.Url.ToString(), 
                QuickLinkGroupType.General, 
                null, 
                Privacy.Private);

            // Display a notification message.
            ScriptManager.RegisterStartupScript(this.UpdatePanel, this.UpdatePanel.GetType(), e.CommandName, "ExecuteOrDelayUntilScriptLoaded(FavouriteImageButton_AddMyLink_Clicked, \"sp.js\");", true);
            break;

        case FavouriteButtonCommandNames.RemoveMyLink:
            long id;

            if (long.TryParse((string)e.CommandArgument, out id))
            {
                // Delete the link.
                QuickLink quickLink = currentUser.QuickLinks[long.Parse((string)e.CommandArgument)];
                quickLink.Delete();

                // Display a notification message.
                ScriptManager.RegisterStartupScript(this.UpdatePanel, this.UpdatePanel.GetType(), e.CommandName, "ExecuteOrDelayUntilScriptLoaded(FavouriteImageButton_RemoveMyLink_Clicked, \"sp.js\");", true);
            }
            else
            {
                throw new ArgumentNullException("e.CommandArgument", "\"{0}\" is not a valid QuickLink ID. The QuickLink could not be removed from the list.");
            }
            break;
    }
}
0
rf51971 On

Add the following function to your master page:

function addlink(){
    t=document.title;
    u=escape(location.href);

    var q = window.location.protocol + "//" + window.location.host + "/_vti_bin/portalapi.aspx?cmd=PinToMyPage&ListViewURL=" + u + "&ListTitle=" + t + "&IsDlg-1"; // + "&ReturnUrl=" + u;

    location.href = q;

}

Then add your anchor tag:

<a href='javascript:addlink()'>Add this Page</a>