Skip to content

Instantly share code, notes, and snippets.

@vgrem

vgrem/entityeditorutils.js

Last active Dec 15, 2015
Embed
What would you like to do?
Expand Group entity to to individual Users entities for People Editor
//Expand Group entity to to individual Users entities
function expandGroupEntity(groupEntity, fnCallback) {
var context = new SP.ClientContext.get_current();
var groups = context.get_web().get_siteGroups();
var groupId = getSPGroupIdForEntity(groupEntity);
var group = groups.getById(groupId);
this.users = group.get_users();
context.load(users);
context.add_requestSucceeded(onLoaded);
context.add_requestFailed(onFailure);
context.executeQueryAsync();
function onLoaded() {
var userEntities = '<Entities>';
var count = users.get_count();
for (i = 0; i < count; i++) {
var userItem = users.itemAt(i);
userEntities += UserEntryToXml(userItem);
}
userEntities += '</Entities>';
var results = GetEntities(userEntities);
fnCallback(groupEntity,results);
}
function onFailure(sender, args) {
fnCallback(groupEntity,null);
}
}
//Create Entity xml fron SPUser
function UserEntryToXml(userItem)
{
return '<Entity Key="' + userItem.get_loginName() + '" DisplayText="' + userItem.get_title() + '" IsResolved="True" Description="' + userItem.get_loginName() + '"><MultipleMatches /></Entity>';
}
function prepareUserEntities(result,ctx,fnCallback)
{
var entities=GetEntities(result);
if (entities==null)
return;
for(var x=0;x<entities.childNodes.length;x++)
{
var entity=entities.childNodes[x];
if(isSPGroupEntity(entity)) {
expandGroupEntity(entity,fnCallback);
}
else
fnCallback(null,null);
}
}
function getSPGroupIdForEntity(entity)
{
var groupId = -1;
var keys = EntityEditor_SelectNodes(entity,"Key");
for (var i=0;i<keys.length;i++) {
if(keys[i].firstChild.nodeValue == "SPGroupID") {
groupId = parseInt(keys[0].nextSibling.firstChild.nodeValue);
break;
}
}
return groupId;
}
function isSPGroupEntity(entity)
{
var isSPGroupEntity = false;
var keys = EntityEditor_SelectNodes(entity,"Key");
for (var i=0;i<keys.length;i++) {
if(keys[i].firstChild.nodeValue == "SPGroupID") {
isSPGroupEntity = true;
break;
}
}
return isSPGroupEntity;
}
var EntityEditorCallback = (function () {
var EntityEditorCallbackOrig = EntityEditorCallback;
return function() {
var result = arguments[0];
var ctx = arguments[1];
prepareUserEntities(result,ctx,function (groupEntity,userEntities) {
if (groupEntity != null) {
result = result.replace(EntitytoHtml(groupEntity),UserEntitiestoHtml(userEntities));
}
EntityEditorCallbackOrig(result, ctx);
});
};
})();
//Serialize user entities to Html
function UserEntitiestoHtml(entities)
{
var entityHtml = '';
for(i=0;i<entities.childNodes.length;i++)
entityHtml += EntitytoHtml(entities.childNodes[i]);
return entityHtml;
}
//Serialize entity to Html
function EntitytoHtml(entity)
{
var entityHtml = EntityEditor_XmlToString(entity);
entityHtml = entityHtml.replace(/<MultipleMatches\/>/g, '<MultipleMatches />');
return entityHtml;
}
//Entity Editor Helper methods
//select nodes from xml entity
function EntityEditor_SelectNodes(xmlNode, tagName)
{ULSGjk:;
//if(document.implementation && document.implementation.createDocument)
//{
var elems=xmlNode.getElementsByTagName(tagName);
if(elems.length > 0)
return elems;
return null;
//}
//else
//{
// return xmlNode.selectNodes(tagName);
//}
}
//Serialize Xml to String
function EntityEditor_XmlToString(xmlData) {
var xmlString;
//IE
if (window.ActiveXObject){
xmlString = xmlData.xml;
}
// code for Mozilla, Firefox, Opera, etc.
else{
xmlString = (new XMLSerializer()).serializeToString(xmlData);
}
return xmlString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.