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

发布于 2024-10-17 00:07:30 字数 684 浏览 1 评论 0原文

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

[这篇博文]给出这个问题的解决方案,但我在“添加链接”对话框 (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 字段?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

や三分注定 2024-10-24 00:07:30

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

这样做的好处是添加链接现在只需一键式过程,缺点是用户没有机会重命名链接或将其分配给组(就我个人而言,我已将组从无论如何,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;
    }
}

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;
    }
}
樱娆 2024-10-24 00:07:30

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

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文