public static void ProcessItem(ListItem item, ClientContext _context, ref List<PeoplePickerListOutput> lstPeoplepickeroutput)
{
PeoplePickerListOutput Peoplepickeroutput = new PeoplePickerListOutput();
try
{
totalCount++;
Logger.LogInfoMessage("item id : " + item.Id);
//Get Web
Web web = _context.Web;
_context.Load(web);
_context.Load(item);
_context.Load(item.ParentList);
_context.Load(item.File);
_context.Load(item.Folder);
_context.ExecuteQuery();
Peoplepickeroutput.ListName = item.ParentList.Title;
Peoplepickeroutput.WebUrl = web.Url;
Peoplepickeroutput.ItemID =Convert.ToString(item.Id);
StringBuilder usersString = new StringBuilder();
StringBuilder groupsString = new StringBuilder();
int itemID = item.Id;
FieldUserValue fuLastModifiedUser = (FieldUserValue)item["Editor"];
string lastModifiedUser = fuLastModifiedUser.LookupValue;
DateTime lastModifiedTimeStamp = Convert.ToDateTime(item["Modified"].ToString());
XDocument xmlDoc = new XDocument();
string fileName = item.File.Name;
string fileServerRelativeUrl = item.File.ServerRelativeUrl;
string folderServerRelativeUrl = fileServerRelativeUrl.Substring(0, fileServerRelativeUrl.LastIndexOf("/"));
string upnPrefix = ConfigurationManager.AppSettings["UpnPrefix"].ToString();
// Approach to read File contents depends on Auth Model chosen
if (Program.UseAppModel == true)
{
string originalFileContents = SafeGetFileAsString(web, item.File.ServerRelativeUrl);
if (String.IsNullOrEmpty(originalFileContents))
{
Logger.LogErrorMessage(String.Format("Could not get contents of file: {0}", item.File.ServerRelativeUrl), false);
return;
}
xmlDoc = XDocument.Load(new StringReader(originalFileContents));
}
else
{
FileInformation fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(_context, item.File.ServerRelativeUrl);
xmlDoc = XDocument.Load(XmlReader.Create(fileInformation.Stream));
}
bool fileupdated = false;
Logger.LogInfoMessage(String.Format("Got InfoPath Form File {0} from{1} Library", fileName, listName), true);
// convert XDocument into string
string xmlData = xmlDoc.ToString();
//Logger.LogInfoMessage(String.Format("Xml Data {0}", xmlData), true);
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>";
xmlData = xml + xmlData;
//Get top nodes
string xmlTopData = xmlData.Substring(0, xmlData.IndexOf("<my"));
if (xmlData.ToLower().Contains("<?mso-infopathsolution"))
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmlData);
//XPathNavigator class provides a set of methods used to modify nodes and values in an XML document
XPathNavigator navItem = xDoc.CreateNavigator();
//Redefine NameSpaceManager (Generate the NameSpace manager for this item)
navItem.MoveToFollowing(XPathNodeType.Element);
XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());
foreach (var ns in navItem.GetNamespacesInScope(XmlNamespaceScope.All))
{
if (ns.Key == String.Empty)
{
nsManager.AddNamespace("def", ns.Value);
}
else
{
nsManager.AddNamespace(ns.Key, ns.Value);
}
}
XPathNodeIterator nodes = navItem.Select("//pc:Person", nsManager);
foreach (XPathNavigator node in nodes)
{
try
{
XPathNavigator nav = node.SelectSingleNode("pc:DisplayName", nsManager);
string displayName = nav == null ? string.Empty : nav.Value;
nav = node.SelectSingleNode("pc:AccountId", nsManager);
string accountId = nav == null ? string.Empty : nav.Value;
nav = node.SelectSingleNode("pc:AccountType", nsManager);
string accountType = nav == null ? string.Empty : nav.Value;
string samaccountname = string.Empty;
if (accountType.ToLower().Contains("user"))
{
if (accountId.Contains(@"\"))
{
samaccountname = accountId.Substring(accountId.LastIndexOf(@"\") + 1);
usersString.Append(samaccountname);
}
}
else if (accountType.ToLower().Contains("group"))
{
Logger.LogInfoMessage(String.Format("Found group: {0}", accountId), true);
//Peoplepickeroutput.AccountType = "Group";
// TO DO: Convert group id into MT compatiable
if (accountId.Contains("|"))
{
samaccountname = accountId.Substring(accountId.LastIndexOf(@"|") + 1);
groupsString.Append(samaccountname).Append(Constants.OutPutreportSeparator);
}
}
if (!accountId.ToLower().Contains(upnPrefix) && !string.IsNullOrEmpty(samaccountname))
{
if (dictUserUpns.ContainsKey(samaccountname))
{
string upn = dictUserUpns[samaccountname];
node.SelectSingleNode("pc:AccountId", nsManager).SetValue(upn);
fileupdated = true;
//Peoplepickeroutput.NewUPN = upn;
usersString.Append(Constants.OutPutreportSeparator).Append(upn).Append(Constants.OutPutreportSeparator);
}
else
{
try
{
string upn = GetUPN(accountType, samaccountname);
string newUPN = string.Empty;
if (!string.IsNullOrEmpty(upn))
{
newUPN = upnPrefix + upn;
dictUserUpns.Add(samaccountname, newUPN);
node.SelectSingleNode("pc:AccountId", nsManager).SetValue(newUPN);
fileupdated = true;
//Peoplepickeroutput.NewUPN = newUPN;
usersString.Append(Constants.OutPutreportSeparator).Append(newUPN).Append(Constants.OutPutreportSeparator);
}
else
{
Logger.LogInfoMessage(String.Format("UPN not found for {0}", samaccountname), true);
}
}
catch (Exception ex)
{
Logger.LogErrorMessage(String.Format("ProcessItem() failed for InfoPath Form Library [{0}] of Web [{1}]: Reason={2}; Error={3}", listName, web.Url,
"LDAP Query Processing not successful.",
"[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false);
lstPeoplepickeroutput.Add(LogExceptionMessages(Peoplepickeroutput, ex));
return;
}
}
}
}
catch (Exception ex)
{
Logger.LogErrorMessage(String.Format("ProcessItem() failed for InfoPath Form Library [{0}] of Web [{1}]: Reason={2}; Error={3}", listName, webUrl,
"failed while iterating nodes.",
"[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false);
lstPeoplepickeroutput.Add(LogExceptionMessages(Peoplepickeroutput, ex));
return;
}
}
if (usersString != null && !string.IsNullOrEmpty(Convert.ToString(usersString)))
{
Peoplepickeroutput.Users = Convert.ToString(usersString).TrimEnd(';');
}
if (groupsString != null && !string.IsNullOrEmpty(Convert.ToString(groupsString)))
{
Peoplepickeroutput.Groups = Convert.ToString(groupsString).TrimEnd(';');
}
if (fileupdated)
{
string updateXml = xmlTopData + navItem.OuterXml;
string fileUrl = string.Empty;
using (MemoryStream memoryStream = new MemoryStream())
{
StreamWriter writer = new StreamWriter(memoryStream);
writer.Write(updateXml);
writer.Flush();
memoryStream.Position = 0;
Logger.LogInfoMessage(String.Format("Saving contents of InfoPath File [{0}] of Web [{1}] ...", fileName, webUrl), false);
// Approach to save File contents depends on Auth Model chosen
if (Program.UseAppModel == true)
{
Folder targetFolder = null;
Logger.LogInfoMessage(String.Format("Getting folder of File [{0}] of Web [{1}] ...", fileName, webUrl), false);
try
{
targetFolder = web.GetFolderByServerRelativeUrl(folderServerRelativeUrl);
web.Context.Load(targetFolder);
web.Context.ExecuteQuery();
Logger.LogInfoMessage(String.Format("Got folder"), false);
}
catch (Exception ex)
{
Logger.LogErrorMessage(String.Format("ProcessItem() failed for File [{0}] in the Form Library {1}] of Web [{2}]: Reason={3}; Error={4}", fileName, listName, webUrl,
"Upload Folder was not Found.",
"[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false);
lstPeoplepickeroutput.Add(LogExceptionMessages(Peoplepickeroutput, ex));
return;
}
Logger.LogInfoMessage(String.Format("Uploading file [{0}] ...", fileName), false);
try
{
targetFolder.UploadFile(fileName, memoryStream, true);
Logger.LogInfoMessage(String.Format("Uploaded file"), false);
}
catch (Exception ex)
{
Logger.LogErrorMessage(String.Format("ProcessItem() failed for File [{0}] in the Form Library {1}] of Web [{2}]: Reason={3}; Error={4}", fileName, listName, webUrl,
"File Upload Failed.",
"[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false);
lstPeoplepickeroutput.Add(LogExceptionMessages(Peoplepickeroutput, ex));
return;
}
}
else
{
try
{
fileUrl = String.Format("{0}/{1}", folderServerRelativeUrl, string.Format(fileName, lastModifiedTimeStamp.ToString()));
Microsoft.SharePoint.Client.File.SaveBinaryDirect(_context, fileUrl, memoryStream, true);
}
catch (Exception ex)
{
Logger.LogErrorMessage(String.Format("ProcessItem() failed for File [{0}] in the Form Library {1}] of Web [{2}]: Reason={3}; Error={4}", fileName, listName, webUrl,
"File Upload Failed.",
"[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false);
lstPeoplepickeroutput.Add(LogExceptionMessages(Peoplepickeroutput, ex));
return;
}
}
Logger.LogInfoMessage(String.Format("Saved contents of File [{0}] in FormLibrary [{1}] of Web [{2}]", fileUrl, listName, webUrl), true);
try
{
_context.Load(item);
_context.ExecuteQuery();
item["Editor"] = fuLastModifiedUser;
item["Modified"] = lastModifiedTimeStamp.ToString();
item.Update();
_context.ExecuteQuery();
Peoplepickeroutput.Status = Constants.SuccessStatus;
Peoplepickeroutput.ErrorDetails = Constants.NotApplicable;
}
catch (Exception ex)
{
Logger.LogErrorMessage(String.Format("ProcessItem() failed for InfoPath Form Library [{0}] of Web [{1}]: Reason={2}; Error={3}", listName, webUrl,
"ited update failed for old Editor & Modified.",
"[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false);
lstPeoplepickeroutput.Add(LogExceptionMessages(Peoplepickeroutput, ex));
return;
}
//Logger.LogInfoMessage(String.Format("Updated Item with previous Editor [{0}] and Modified Timestamp [{1}] in FormLibrary [{2}] of Web [{3}]",
// lastModifiedUser, lastModifiedTimeStamp.ToString(), listName, webUrl), true);
}
}
else
{
Peoplepickeroutput.Status = Constants.NoUpdateRequired;
Peoplepickeroutput.ErrorDetails = Constants.NotApplicable;
}
lstPeoplepickeroutput.Add(Peoplepickeroutput);
}
}
catch (Exception ex)
{
Logger.LogErrorMessage(String.Format("ProcessItem() failed for InfoPath Form Library [{0}] of Web [{1}]: Reason={2}; Error={3}", listName, webUrl,
"failed while processing item.",
"[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false);
return;
}
}