我最终使用对象模型来创建我的链接(与弹出对话框相对)。
这样做的好处是添加链接现在只需单击一下即可,坏处是用户没有机会重命名链接或将其分配给组(就我个人而言,我已将组从无论如何 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;
}
}