2

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

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

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

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

谁能建议我如何从我的母版页调用这些函数中的一个/一些,以便打开“添加链接”对话框并预先填充标题和 URL 字段?

4

2 回答 2

2

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

这样做的好处是添加链接现在只需单击一下即可,坏处是用户没有机会重命名链接或将其分配给组(就我个人而言,我已将组从无论如何 UI,因为我们不需要它们,所以这对我来说不是问题)。

对于那些感兴趣的人,我创建了一个小用户控件,其中包含一个 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;
    }
}
于 2011-03-01T14:48:01.800 回答
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>
于 2011-04-20T14:42:14.283 回答