Posts

Showing posts from February, 2015

Recursive Folder Creation method for SharePoint

Image
public static class SPFolderExtensions { /// <summary> /// Ensure SPFolder /// </summary> /// <param name="web"></param> /// <param name="listTitle"></param> /// <param name="folderUrl"></param> /// <returns></returns> public static SPFolder CreateFolder(this SPWeb web, string listTitle, string folderUrl) { if (string.IsNullOrEmpty(folderUrl)) { throw new ArgumentNullException("folderUrl"); } var list = web.Lists.TryGetList(listTitle); return CreateFolderInternal(list, list.RootFolder, folderUrl); } /// <summary> /// Creates the folder internal. /// </summary> /// <param name="list">The list.</param> /// <param name="parentFolder">The parent folder.</param> ...

Convert XML data to dynamic (ExpandoObject) Object

Image
public static IEnumerable<dynamic> GetExpandoFromXml(string file, string descendantid) { var expandoFromXml = new List<dynamic>(); var doc = XDocument.Load(file); var nodes = doc.Root.Descendants(descendantid); foreach (var element in doc.Root.Descendants(descendantid)) { dynamic expandoObject = new ExpandoObject(); var dictionary = expandoObject as IDictionary<string, object>; foreach (var child in element.Descendants()) { if (child.Name.Namespace == "") dictionary[child.Name.ToString()] = child.Value.Trim(); } yield return expandoObject; } }

Generic Method to Get an Element value of XML Document

1. Generic Method public static T GetElementValue<T>(this XElement element) { if (element == null) return default(T); if (string.IsNullOrEmpty(element.Value)) return default(T); if (typeof(T) == typeof(string)) return (T)Convert.ChangeType(element.Value, typeof(T)); PropertyInfo[] properties = typeof(T).GetProperties(); Type underlyingType = typeof(T); if (properties.Count() == 2 && string.Equals(properties[0].Name, "HasValue", StringComparison.InvariantCultureIgnoreCase)) underlyingType = properties[1].PropertyType; MethodInfo method = underlyingType.GetMethod("Parse", new[] { typeof(string) }); var newObject = (T)method.Invoke(null, new object[] { element.Value }); return newObject; } 2. Method Call var xElement = XElement.Load("Data.xml"); var siteUrl = xElement.Element("Sit...