0

I'm trying to save a user profile field back to SharePoint using Javascript running within the portal. The request is kicking back a 400 error with no real descriptions of what the error might be. From what I understand, in SharePoint Online, using these older web services is the only way to save profile fields back to a user from a page.

Below is the code running....

function SaveUserProperty(accountName, propertyName, propertyValue) {

writeResult("Setting User Property for " + accountName);

// this variable contains the XML that is passed to the SOAP web service
//  about which account will have which property modified

var newData = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3. xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ModifyUserPropertyByAccountName xmlns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService"><accountName>' + accountName + '</accountName><newData><PropertyData><IsPrivacyChanged>false</IsPrivacyChanged><IsValueChanged>true</IsValueChanged><Name>' + propertyName + '</Name><Privacy>Public</Privacy><Values><ValueData><Value xsi:type="xsd:string">' + propertyValue + '</Value></ValueData></Values></PropertyData></newData></ModifyUserPropertyByAccountName></soap:Body></soap:Envelope>'

// this is the actual call to the web service and method responsible
//   for modifying user profile property
$.ajax({  
url: '/_vti_bin/UserProfileService.asmx', 
beforeSend: function(xhr) {
        xhr.setRequestHeader("SOAPAction", "http://microsoft.com/webservices/SharePointPortalServer/UserProfileService/ModifyUserPropertyByAccountName");
    }, 
type: "POST",  
dataType: "xml",  
data: newData,  
complete: completeSave,  
contentType: "text/xml; charset=\"utf-8\"" 
}); 
return false; 
}
2

There is a typo in SOAP message (newData variable). Bellow is valid SOAP message for ModifyUserPropertyByAccountName operation:

var msg = "<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><ModifyUserPropertyByAccountName xmlns='http://microsoft.com/webservices/SharePointPortalServer/UserProfileService' ><accountName>" + accountName + "</accountName><newData><PropertyData><IsPrivacyChanged>false</IsPrivacyChanged><IsValueChanged>true</IsValueChanged><Name>" + propertyName + "</Name><Privacy>NotSet</Privacy><Values><ValueData><Value xsi:type='xsd:string'>" + propertyValue + "</Value></ValueData></Values></PropertyData></newData></ModifyUserPropertyByAccountName></soap:Body></soap:Envelope>";

It is highly recommended to leverage SPServices library when working with SharePoint SOAP services since it hides all intricacies and complexity related with SOAP services in SharePoint.

How to use ModifyUserPropertyByAccountName operation:

var propertyData = "<PropertyData>" +
    "<IsPrivacyChanged>false</IsPrivacyChanged>" +
    "<IsValueChanged>true</IsValueChanged>" +
    "<Name>" + propName + "</Name>" +
    "<Privacy>NotSet</Privacy>" +
    "<Values><ValueData><Value xsi:type=\"xsd:string\">" + propValue + "</Value></ValueData></Values>" +
  "</PropertyData>";
$().SPServices({
  operation: "ModifyUserPropertyByAccountName",
  async: false,
  webURL: "/",
  accountName: userId,
  newData: propertyData,
  completefunc: function (xData, Status) {
    var result = $(xData.responseXML);
  }
}); 

Note: accountName parameter has the following format: domain\userId for on-prem and i:0#.f|membership|<federated ID> for SharePoint Online

1

The SOAP web services have been deprecated. Even if you did get it working there will come a point when it won't and Microsoft will no longer support it.

I am sure you have seen the articles that describe your options from JavaScript: http://msdn.microsoft.com/en-us/library/office/jj920104(v=office.15).aspx

This is a great reference for common tasks: http://msdn.microsoft.com/en-us/library/office/jj163800(v=office.15).aspx#bkmk_CommonTasks

In your case, setting a property (aside from picture) is not possible from the client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.