Posts

Showing posts with the label C#

How to get all the Sub-Sites under a SharePoint Site Collection (C#)?

Image
Here is the C# code for getting current site + all the sites under under current site collection. For example, here is my site structure: And the output will be as sown below: URL Title Root Site url Root Site Title Sub-site1 url Root Site Title>Sub-site1 title Sub-site1.1 url Root Site Title>Sub-site1 title>Sub-site1.1 title Sub-site2 url Root Site Title>Sub-site2 title Here is the code:                         SPWeb web = SPContext.Current.Web;                         Dictionary<string, string> AllSubsites = new Dictionary<string, string>();                         SPSecurity.RunWithElevatedPrivileges(delegate ()                         {                 ...

LINQ: Dynamic Groupby, OrderBy, and Dynamic Aggregates.

How to dynamically achieve groupby, orderby and aggregates using LINQ ? huh.. what..? Is it is possible.. yes it is.. Here is the solution. Here are some of our helper methods: private static object GetPropertyValue(object obj, string propertyName)         {             return obj.GetType().GetProperty(propertyName).GetValue(obj, null);         } private static double GetPropertyValueDouble(object obj, string propertyName)         {             double ret = Convert.ToDouble(obj.GetType().GetProperty(propertyName).GetValue(obj, null));             return ret;         } Consider below sample example:  public class test     {         public string City { get; set; }         public string Name { get; set; }         public string State {...

C#: How to dynamically invoke a method from a Class, when you know the ClassName and Method Name as string

How to invoke a method from a Class, when you know the ClassName and Method Name as string in C#. Here is the Solution. Consider below helper method: (change the namespace, version and public key tokens accordingly)  public static Type GetTypeFromClassName(string ClassName)         {             string ss = string.Format("Solutions.Common.{0}, Solutions.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3a6a3aa5a3aed1e3", ClassName);             Type typeYouWant = Type.GetType(ss);             if (typeYouWant != null)             {                 return typeYouWant;             }             return null;         } For explanation of above method, read my below post. Creating Object of a Class ...

Creating Object of a Class dynamically when you have name of the Class as string in C#?

How to create an object of a class dynamically when you have the name of class as string in c#. Here is the solution. First, get the type of the class. Below helper method Returns the Type of your class. Here Solutions.Common is the Namespace of your class. To get the Version and PublicKeyToken, go to run (window+r) , type assembly, search for your assembly , right click , properties and get these details.  public static Type GetTypeFromClassName(string ClassName)         {             string ss = string.Format("Solutions.Common.{0}, Solutions.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3a6a3aa5a3aed1e3", ClassName);             Type typeYouWant = Type.GetType(ss);             if (typeYouWant != null)             {                 return typeYouWant;     ...