如何在列表的自定义newform的页面源中使用c#代码来更新它的某些字段 [英] how to use c# code in the page source of custom newform of a list to update some fields of it

查看:32
本文介绍了如何在列表的自定义newform的页面源中使用c#代码来更新它的某些字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was looking for a solution where I'll populate the value from one list and display it to the another list. I have now got some code but not sure how use it.

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

I have to now use some c# code into page source of custom newform of a list. This code will actually retrieve the user information and update to the field in the custom newform in the list.

以下 C# 代码我想使用共享点设计器在 newform 页面源中使用

Following C# code I want to use in newform page source using sharepoint designer

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

请帮助我完成这项工作.

Please help me to get this work.

推荐答案

SharePoint Designer 删除了各种类型的代码,包括所有 C#,以防止意外引入漏洞.要使用 C# 代码,您需要使用 VIsual Studio 创建和部署解决方案包.相反,您最好的选择可能是使用 JavaScript.这里是 2013 年的文档这里是一个简化 2010 年数据检索的实用程序这里是一些代码可以让你非常接近你想要做的事情.

SharePoint Designer removes various types of code, including all C#, in order to prevent vulnerabilities from accidentally being introduced. To use your C# code you will need to create and deploy a solution package with VIsual Studio. Instead, your best bet is probably to use JavaScript. Here is documentation for 2013, here is a utility to simplify retrieving the data in 2010, and here is some code that will get you very close to what you're trying to do.

链接失效时的代码副本:

Copy of the code in case that link dies:

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

这篇关于如何在列表的自定义newform的页面源中使用c#代码来更新它的某些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆