SharePoint Designer 删除各种类型的代码,包括所有 C#,以防止意外引入漏洞。要使用 C# 代码,您需要使用 VIsual Studio 创建和部署解决方案包。相反,您最好的选择可能是使用 JavaScript。这是 2013 的文档,这是一个用于简化在 2010 中检索数据的实用程序,还有一些代码可以让您非常接近您想要做的事情。
链接失效时的代码副本:
<script type="text/javascript">
// ensure system stuff is loaded before we start calling client object model
ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
// create context variables
var context = null;
var web = null;
var currentUser = null;
// this function calls object model to determine current user name
function getWebUserData() {
context = new SP.ClientContext.get_current();
web = context.get_web();
currentUser = web.get_currentUser();
currentUser.retrieve();
context.load(web);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
Function.createDelegate(this, this.onFailureMethod));
}
// this function gets called if we get current user name successfully
function onSuccessMethod(sender, args) {
var loginName = web.get_currentUser().get_loginName();
// this call requests the value for property named "Title" for current user name
GetUserProperty(loginName, "Title");
}
// yes, things failed; I ignore it here but you can display an alert
function onFailureMethod(sender, args) {
// Unable to find user profile
}
// function which retrieves the value of the property
function GetUserProperty(accountName, propertyName) {
// constructing the call to a user profile using web services
var soapMessage = '<?xml version="1.0" encoding="utf-8"?>'
+ '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
+ '<soap:Body>'
+ ' <GetUserPropertyByAccountName
xmlns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService">'
+ ' <accountName>' + accountName + '</accountName>'
+ ' <propertyName>' + propertyName + '</propertyName>'
+ ' </GetUserPropertyByAccountName>'
+ ' </soap:Body>'
+ '</soap:Envelope>'
// making a call with jQuery
$.ajax({
url: '/_vti_bin/UserProfileService.asmx',
type: "POST",
dataType: "xml",
data: soapMessage,
complete: displayProfileProperty,
contentType: "text/xml; charset=\"utf-8\""
});
return false;
}
// things went well and we get results back
function displayProfileProperty(xmlHttpRequest, status)
{
// the result is burried in XML markup so we look for the right node
$(xmlHttpRequest.responseXML).find('Values').each(function()
{
// get the text property of the node and display it
var name = $(this).find('Value').text();
alert(name);
});
}