Saturday, November 27, 2010

HTTP Module in sharepoint

Share/Save/Bookmark

In one of the scenario i got to replace the application page(new.aspx) with my custom application page(customnew.aspx). so quickly i got the new application page but then started wondering how to link this page to sharepoint without adding a new link but just replacing the existing link?
here is one of the solution implementing IHTTPModule..

public class Class1 : IHttpModule
    {
        public void Init(HttpApplication app)
        { 
            app.PreRequestHandlerExecute +=new EventHandler(app_PreRequestHandlerExecute);

        
        }
        void app_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            if(context.Request.Path.Contains("/_layouts/new.aspx"))
            {
                string url = "customnew.aspx";
                if (!context.Request.QueryString.Equals("{}"))
                {
                    url = url + '?' + context.Request.QueryString;
                }
                context.Response.Redirect(url);
            }
        }
        public void Dispose()
        {
        }
}


Add the reference of this new httpmodule in web.config under HttpModules.

  Subscribe

Thursday, November 4, 2010

Programmatically read MetaData Store

Share/Save/Bookmark

Here is the code that you can reuse to read the complete managed metadata store or taxonomy in SharePoint for a specific site collection.
Below code writes the metadata to a xml file following a very basic schema also presents it on console in a formatted style.

For basic understanding, the classes or the taxonomy hierarchy(new in SharePoint) is like below

Taxonomy Session 
            |
    Term Store
            |
       Group
            |
      TermSet
            |
        Term

Microsoft.SharePoint.Taxonomy is the dll that's used here to play around with SharePoint taxonomies.


namespace MetaDataReader
{
    class Program
    {
        static string fmt = "---------"; // used for formatting the hierarchy while displaying on console
        static XmlTextWriter writer = new XmlTextWriter("metadataStore.xml", null);
        public static void Main(string[] args)
        {
            try
            {
                SPSite site = new SPSite("http://mywebappurl");
                TaxonomySession txSesssion = new TaxonomySession(site);
                writer.WriteStartDocument();
                foreach (TermStore store in txSesssion.TermStores)
                {
                    Console.WriteLine("TermSTORE : " + store.Name);
                    writer.WriteStartElement("TermStore");
                    writer.WriteStartElement("Name");
                    writer.WriteString(store.Name);
                    writer.WriteEndElement();
                  
                    foreach (Group g in store.Groups)
                    {
                        Console.WriteLine("...TermGROUP : " + g.Name);
                        writer.WriteStartElement("TermGroup");

                        writer.WriteStartElement("Name");
                        writer.WriteString(g.Name);
                        writer.WriteEndElement();

                        foreach (TermSet tSet in g.TermSets)
                        {
                            Console.WriteLine("......TermSET : " + tSet.Name);
                            writer.WriteStartElement("TermSet");

                            writer.WriteStartElement("Name");
                            writer.WriteString(tSet.Name);
                            writer.WriteEndElement();

                            foreach (Term t in tSet.Terms)
                            {
                                Console.WriteLine(fmt + "TERM :" + t.Name);
                                writer.WriteStartElement("Term");

                                writer.WriteStartElement("Name");
                                writer.WriteString(t.Name);
                                writer.WriteEndElement();
                                getTermsRecursive(t);
                                writer.WriteEndElement();
                            }
                            writer.WriteEndElement();
                        }
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }
                writer.WriteEndDocument();
                writer.Close();
            }
            catch (Exception ex)
            {//Do not swallow ;}
        }

        public static void getTermsRecursive(Term t)
        {
            if (t.Terms.Count > 0)
            {
                fmt += "---";
                foreach (Term sTerm in t.Terms)
                {
                    Console.WriteLine(fmt + "TERM :" + sTerm.Name );
                    writer.WriteStartElement("Term");
                    writer.WriteStartElement("Name");
                    writer.WriteString(sTerm.Name);
                    writer.WriteEndElement();
                  
                    getTermsRecursive(sTerm);
                    writer.WriteEndElement();
                }
            }
            else
            {
                fmt.Remove(fmt.Length-3);
                return;
            }
        }
    }
}



 Subscribe

Monday, October 4, 2010

Configure BCS in Sharepoint 2010

Share/Save/Bookmark

Lets get started with BCS in sharepoint 2010 with a small demo thats compiled as step by step instruction:

1. Open any MOSS site using Sharepoint Designer 2010

2. Select External content Types on left menu under Site Objects



3. Click on External Content Type, (seen on ribbon) to create a new one

4. Provide name and select  click here to discover external system where the data is residing to add external data source system





5. Click on Add connection to add a data source. Here our data source type is SQL server as the external data is a table in SQL server.





6. Provide server properties to connect to sql server



7. After successfully connecting to the new external data source through the data connection, add operations(read,insert,update,delete) by right clicking on the table(here EMP) that needs to be pulled through BCS.

8. Create New Read List Operation


9. Run through the wizard where operation parameters (Filter/Return) can be specified. Note the errors and warnings that are self explanatory for which, properties of each element can be changed accordingly to resolve them.



10.   For any external content type to be of use, it should at least have the read list and read item operations created. The same information can be seen in the External Content Type Operations panel.



11.   Now Create Read Item operation(follow steps 8 & 9)

12.   Save

13.   Open CentralAdmin to see the new External Content Type that is created




14.   Click on Set Object Permissions or Set Metadata Store Permissions to provide relevant permissions to the users who ever should access the external data pulled out by the external List, created based on the External content type. Note that missing this step would give Access Denied to Business Connectivity Services when tried to view external list. 



15.   Create External List using designer to see the output that we are waiting for. External list can also be created through the site.




  16.   Navigate to the External list (here DemoExCTList) in the site and here is the external data in sharepoint.





  Subscribe