how to use c# code in the page source of custom ne

2019-09-09 22:25发布

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条回答
Viruses.
2楼-- · 2019-09-09 23:20

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

查看更多
登录 后发表回答