Thursday, March 31, 2011

Get All AD Groups using LDAP in C#

Issue: LDAP Query not pulling all the Active Directory Groups (including subgroups) available in the given domain.

Solution:
To retrieve a set of results that is larger than 1000 items, you must set SizeLimit to its default value (zero) and set PageSize to a value that is less than or equal to 1000.

Concept Source: Click here


public static DataTable GetAllActiveDirectoryGroups(string ldapServer, string ldapUserName, string ldapPassWord)
        {
            DataTable dt = new DataTable();
            DataRow dr;
 
 
            DirectoryEntry de = new DirectoryEntry(ldapServer);
            de.Username = ldapUserName;
            de.Password = ldapPassWord;
            DirectorySearcher deSearch = new DirectorySearcher(de.Path);
            
            SearchResultCollection results;
            dt.Columns.Add("GroupName");
            try
            {
                deSearch.Filter = ("(&(objectCategory=group))");
                deSearch.SearchScope = SearchScope.Subtree;
                //deSearch.SizeLimit = 10000;
                deSearch.PageSize = 1000;
                results = deSearch.FindAll();
 
                foreach (SearchResult result in results)
                {
                    dr = dt.NewRow();
                    dr["GroupName"] = result.Properties["cn"][0].ToString();
                    dt.Rows.Add(dr);
                }
                de.Close();
 
 
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (deSearch != null)
                {
                    deSearch.Dispose();
                }
                if (de != null)
                {
                    de.Dispose();
                }
            }
            
            return dt;
 
        }
 
 
Code can be downloaded here: Download

Sunday, March 13, 2011

Security Exception: The application attempted to perform an operation not allowed by the security policy

While Working on a VisualWebPart with Telerik Controls I got a Security Exception. I searched many blogs and articles, and found some information for configuring Trust Level for Code in Microsoft .NET Framework 2.0 Configuration (can be found in Administration Tools, if the .Net Framework SDK 2.0 is installed in your machine) section. But, unfortunately it didn't help me much. Finally I tried in IIS Trust Level settings and changed it to Medium level that worked just fine. Thought it helps other so posted here, below are the details of it.

Environment I tried:
SharePoint 2010, IIS 7, Telerik Controls (2010 -3 - 1317)
Error Description:
The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.



Solution (It worked for me):
Step 1:
       Go to IIS and select the webapplication you are trying (like SharePoint - 80, in my case).

Step 2:
       In the right side window you can find the options like below, select .Net Trust Levels
Step 3:
      Select the Trust Level to Medium like below.


Step 4:
     Click on Apply to commit the changes, like shown below (it resides in the same window at the right - top)
Now try in your page, the error should go away ( if the problem is with this).


Another Solution FromTelerik Site:
Cause:
Microsoft changed the default setting of the Load User Profile setting of the application pools in Windows 7 and Windows 2008 (the old was True, in IIS7.5 it is False).
Suggested solution:
Open the Advanced Settings of the Application Pool (for the web app you are using) and set the Load User Profile property to True.
Source Link: http://www.telerik.com/help/aspnet-ajax/troubleshooting.html

Other Useful Posts:

Wednesday, March 9, 2011

Get Columns Schema using ADO.Net connection

Get the details of columns for a given table using ADO.Net Connection

public DataTable GetTableColumns(string strConnection, string strTableName)
     {
         //Create connectionstring
         SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder( strConnection);
         SqlConnection con = null;
         try 
         {
             con = new SqlConnection(builder.ConnectionString);
             con.Open();
 
             // As my requirement was to just list out the columns for a table, I have used top 1 *
             SqlCommand cmd =  new SqlCommand("Select top 1 * from " + strTableName, con);
             SqlDataReader rd = cmd.ExecuteReader();
 
             //Get the Schema for the given table
             return rd.GetSchemaTable();
             
         }
         catch (Exception ex)
         {
             throw new Exception(ex.Message + ex.StackTrace);
         }
         finally
         {
             if (con != null)
             {
                 con.Dispose();
             }
         }
 
         
     }
 
Code can be downloaded here: Download

Get Database Tables Schema using ADO.Net Connection

Functionality for reading Database tables' schema using ADO.Net connection

public DataTable GetTables(string strConnection)
     {
         //Create connectionstring
         SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(strConnection);
         SqlConnection con = null;
         DataTable dtTables = new DataTable();
         try
         {
             con = new SqlConnection(builder.ConnectionString);
             con.Open();
 
             //Get the Schema for the tables
             dtTables = con.GetSchema(SqlClientMetaDataCollectionNames.Tables, new string[] { null, null, null, "BASE TABLE" });
         }
         catch (Exception ex)
         {
             throw ex;
         }
         finally
         {
             if (con != null)
             {
                 con.Dispose();
             }
         }
 
         return dtTables;
     }
Code can be downloaded here: Download

Wednesday, March 2, 2011

Find WorkListItems with Worklist Criteria Filter in K2 Blackpearl

While working for an utility to redirect the tasks with some custom functionality I had to search for an option to bring the WorklistItems (tasks) based on some criteria (like Folio or Title) using LIKE operator with K2 API classes WorkflowCriteria and WorkflowCriteriaFilter, but it didn’t work. Googled with some keywords and found few articles, but I could not see any simple solution from them. So did some trials on search criteria on my own finally I could figure out that if we use percentile (%) symbols with LIKE operator it works perfectly. I didn't expect this as the APIs itself should take care (as per my understanding) of this when we use LIKE operator in Criteria classes. Anyway, it is working fine for me. I thought my findings may help some folks so posted that piece of code here.


 //Create a connection string with Integrated mode
    SCConnectionStringBuilder connectionString = new SCConnectionStringBuilder();
        connectionString.Authenticate = true;
        connectionString.Host = "ServerName";
        connectionString.Integrated = true;
        connectionString.IsPrimaryLogin = true;
        connectionString.Port = 5555;

        WorkflowManagementServer workflowServer = new WorkflowManagementServer();
        try
        {
            //Create a connection
            workflowServer.CreateConnection();
            workflowServer.Connection.Open(connectionString.ToString());

            WorklistCriteriaFilter filter = new WorklistCriteriaFilter();
            // Without % symbols it won't bring any data. It is important to add
   filter.AddRegularFilter(WorklistFields.Folio, Comparison.Like, "%srinitest%");
        
WorklistItems listItems = workflowServer.GetWorklistItems(filter);
            foreach (WorklistItem item in listItems)
            {
                //Add a row  to table to show it on form
   //AddRow(item.ProcInstID.ToString(),item.Folio,item.ActivityName,     item.EventName, item.Destination, item.ProcName);

            }
        }
        catch (Exception ex)
        {
            //Handle Exception here
        }
        finally
        {
         if (workflowServer != null && workflowServer.Connection != null)
            {
                //Dispose the connection saftely
                workflowServer.Connection.Dispose();
            }
        }

You can also find the code here Download

     Hope it helps!

Access to XMLHttpRequest at 'from origin has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. .net core angular

Issue: The angular application was getting error from API that the origin has been blocked by CORS policy. Solution: Make sure that the...