Sharepoint 2010 如何在自定义表单的页面源代码中使用c#代码更新列表的某些字段

Sharepoint 2010 如何在自定义表单的页面源代码中使用c#代码更新列表的某些字段,sharepoint-2010,sharepoint-designer,sharepoint-workflow,Sharepoint 2010,Sharepoint Designer,Sharepoint Workflow,我一直在寻找一个解决方案,我将填充一个列表中的值,并将其显示到另一个列表中。我现在有一些代码,但不知道如何使用它 我现在必须使用一些c#代码到自定义列表新形式的页面源代码中。这段代码实际上将检索用户信息并更新到列表中自定义新表单中的字段 下面是我想在使用sharepoint designer的newform页面源代码中使用的C#代码 SPSite _site = SPContext.Current.Site; ServerContext serverContext = ServerC

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

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

下面是我想在使用sharepoint designer的newform页面源代码中使用的C#代码

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

请帮助我完成这项工作。

SharePoint Designer删除各种类型的代码,包括所有C#,以防止意外引入漏洞。要使用C#代码,您需要使用VIsual Studio创建并部署一个解决方案包。相反,您最好的选择可能是使用JavaScript,及

链路失效时的代码副本:

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

//在开始调用客户机对象模型之前,请确保已加载系统内容
ExecuteOrderLayUntilScriptLoaded(getWebUserData,“sp.js”);
//创建上下文变量
var-context=null;
var-web=null;
var currentUser=null;
//此函数调用对象模型以确定当前用户名
函数getWebUserData(){
context=new SP.ClientContext.get_current();
web=context.get_web();
currentUser=web.get_currentUser();
currentUser.retrieve();
加载(web);
context.executeQueryAsync(Function.createDelegate(this,this.onSuccessMethod),
Function.createDelegate(this,this.onFailureMethod));
}
//如果成功获取当前用户名,将调用此函数
函数onSuccessMethod(发送方,参数){
var loginName=web.get_currentUser().get_loginName();
//此调用为当前用户名请求名为“Title”的属性值
GetUserProperty(登录名,“标题”);
}
//是的,事情失败了;我在这里忽略它,但您可以显示警报
函数onFailureMethod(发送方,参数){
//找不到用户配置文件
} 
//用于检索属性值的函数
函数GetUserProperty(accountName,propertyName){
//使用web服务构造对用户配置文件的调用
var soapMessage=''
+ ''
+ ''
+ ' '
+''+accountName+''帐户名
+''+属性名称+''
+ ' '
+ ' '
+ ''
//用jQuery打电话
$.ajax({
url:“/\u vti\u bin/UserProfileService.asmx”,
类型:“POST”,
数据类型:“xml”,
数据:soapMessage,
完成:displayProfileProperty,
contentType:“text/xml;字符集=\“utf-8”
}); 
返回false;
}
//事情进展顺利,我们得到了结果
函数displayProfileProperty(xmlHttpRequest,状态)
{  
//结果隐藏在XML标记中,因此我们寻找正确的节点
$(xmlHttpRequest.responseXML).find('Values').each(function()
{  
//获取节点的文本属性并显示它
var name=$(this.find('Value').text();
警报(名称);
}); 
}