0

我正在寻找一种解决方案,我将从一个列表中填充值并将其显示到另一个列表中。我现在有一些代码,但不知道如何使用它。

我现在必须在自定义新形式列表的页面源中使用一些 c# 代码。此代码实际上将检索用户信息并更新到列表中自定义新表单中的字段。

遵循 C# 代码,我想使用 sharepoint 设计器在 newform 页面源中使用

   SPSite _site = SPContext.Current.Site;
   ServerContext serverContext = ServerContext.GetContext(_site);
   UserProfileManager myUserProfile = new UserProfileManager(serverContext);
   UserProfile currentUserProfile = myUserProfile .GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);

   string departmentName = (string)currentUserProfile["department"].Value;
   string managerName = (string)currentUserProfile["manager"].Value;
   _site.RootWeb.Dispose();
   _site.Dispose();

请帮助我完成这项工作。

4

1 回答 1

0

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); 
}); 
} 

于 2013-11-05T16:07:55.597 回答