通过代码将自定义链接添加到 SharePoint 列表设置页面需要使用 SharePoint 的 REST API 和 CSOM(Client-side Object Model)。以下是一个完整的解决方案:
首先,我们需要创建一个自定义操作,这将在列表设置页面上显示为一个链接。我们可以使用 REST API 来创建这个操作。以下是一个示例代码:
function createCustomAction() {
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/usercustomactions";
var requestHeaders = {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
};
var requestData = {
"__metadata": { "type": "SP.UserCustomAction" },
"Title": "My Custom Link",
"Description": "This is a custom link that will be added to the SharePoint list settings page.",
"Location": "Microsoft.SharePoint.StandardMenu",
"Group": "SiteActions",
"Sequence": 1000,
"Url": "https://www.example.com"
};
return $.ajax({
url: requestUri,
type: "POST",
headers: requestHeaders,
data: JSON.stringify(requestData)
});
}
接下来,我们需要使用 CSOM 将自定义操作添加到列表设置页面。以下是一个示例代码:
using (var clientContext = new ClientContext("https://your-tenant.sharepoint.com/sites/your-site"))
{
var web = clientContext.Web;
var list = web.Lists.GetByTitle("Your List");
var customAction = list.UserCustomActions.Add();
customAction.Title = "My Custom Link";
customAction.Description = "This is a custom link that will be added to the SharePoint list settings page.";
customAction.Location = "Microsoft.SharePoint.StandardMenu";
customAction.Group = "SiteActions";
customAction.Sequence = 1000;
customAction.Url = "https://www.example.com";
customAction.Update();
clientContext.ExecuteQuery();
}
最后,我们需要将自定义操作添加到列表设置页面。以下是一个示例代码:
function addCustomActionToSettingsPage() {
var settingsPageUrl = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/listedit.aspx?List=" + _spPageContextInfo.pageListId;
var requestUri = settingsPageUrl + "?loadCustomActions=true";
var requestHeaders = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
};
return $.ajax({
url: requestUri,
type: "GET",
headers: requestHeaders
}).done(function(response) {
var customActionLink = "<a href='https://www.example.com' target='_blank'>My Custom Link</a>";
var $settingsPage = $(response);
$settingsPage.find("#DeltaPlaceHolderPageTitleInTitleArea").append(customActionLink);
$settingsPage.find("#DeltaPlaceHolderPageTitleInTitleArea").css("display", "inline-block");
$settingsPage.find("#DeltaPlaceHolderPageTitleInTitleArea").css("float", "left");
$settingsPage.find("#DeltaPlaceHolderPageTitleInTitleArea").css("margin-right", "10px");
$("body").html($settingsPage);
});
}
这个解决方案将创建一个自定义操作,并将其添加到列表设置页面上。当用户点击该链接时,将会打开一个新的标签页,链接到指定的 URL。
领取专属 10元无门槛券
手把手带您无忧上云