how to use c# code in the page source of custom newform of a list to update some fields of it

孤街浪徒 提交于 2019-12-13 04:46:37

问题


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.

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.

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.


回答1:


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



来源:https://stackoverflow.com/questions/19667566/how-to-use-c-sharp-code-in-the-page-source-of-custom-newform-of-a-list-to-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!