SharePoint 2010:创建一个书签按钮,将页面添加到“我的链接”

时间:2011-02-10 16:45:47

标签: javascript sharepoint-2010 mysite

我正在尝试在我的母版页上创建一个链接/按钮,当点击该按钮时,会将当前页面添加到用户的我的链接列表中。这只是一种快捷方式,可以让用户无需导航到我的网站并手动添加链接。

[This blog post]提供了解决此问题的方法,但是在“添加链接”对话框(QuickLinksDialog2.aspx)的第二行上出现JavaScript错误,因为 frameElement 属性为null :

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

无论如何, Portal.js 似乎包含“我的链接”页面(_layouts / MyQuickLinks.aspx)用于添加此列表链接的所有功能。

有人可以建议我如何从我的母版页调用这些功能中的一个/一些,以便打开“添加链接”对话框,其中标题和URL字段是预先填好的吗?

2 个答案:

答案 0 :(得分:2)

我最终使用对象模型来创建我的链接(与弹出对话框相对应)。

这样做的好处是添加链接现在只是一次点击过程,缺点是用户没有机会重命名链接或将其分配给一个组(个人而言,我隐藏了无论如何我都不需要它们来自UI,所以这对我来说不是问题。

对于那些感兴趣的人,我创建了一个小的usercontrol,它只包含一个ajaxified按钮,您可以将其放在母版页/页面布局上。我的代码如下:

<强> 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;
    }
}

答案 1 :(得分:2)

将以下功能添加到母版页:

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;

}

然后添加您的锚标记:

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