Tuesday, 2 October 2012

SQL Connection string , with windows Authentication

you can use this to connect to sql server data base whene authentication mode is windows

SqlConnection con = new SqlConnection("Data Source=OMSAI-PC;Initial Catalog=Northwind;Integrated Security = SSPI;")

this works for me......................

Sunday, 30 September 2012

Hosting WCF in consol application

Tomasz Rabiński | zavaz Blog

C# SharePoint VSTO

Hosting WCF service in a console application

There are different ways to host a WCF appliaction. You can host it in:
  • IIS
  • WAS
  • Windows service
  • console application
The last one is very simple to do and I’ll will showYou how to do it.
First, create a new WCF Service Appliaction project. Then open the IService1.cs interface and add a custom method:
1
2
[OperationContract]
int GetSum(int a, int b);
After that, implement this method in the Service1.svc.cs file. I did it like this:
1
2
3
4
public int GetSum(int a, int b)
 {
 return a + b;
 }
Next create a new Console Application project and fill the Main function with this content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Creating a new ServiceHost instance with two base addresses: one for the http and second for the net.tcp
using (ServiceHost sh = new ServiceHost(typeof(WCFAppTest.Service.Service1), new Uri("http://localhost:72/Service"), new Uri("net.tcp://localhost:71/Service")))
 {
 //Adding the endpoints to the ServiceHost
 sh.AddServiceEndpoint(typeof(WCFAppTest.Service.IService1), new NetTcpBinding(), "");
 sh.AddServiceEndpoint(typeof(WCFAppTest.Service.IService1), new WSHttpBinding(), "");
  
 //Creating a new Service Behavior
 ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
 smb.HttpGetEnabled = true;
 sh.Description.Behaviors.Add(smb);
  
 //Opening the ServiceHost for incoming connections
 sh.Open();
  
 System.Console.WriteLine("Waiting for connections...");
 System.Console.ReadLine();
  
 //Close opened ServiceHost object
 sh.Close();
 }
That’s it. Now You have a fully functional WCF Service. TO test it You can create second console project and fill it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using (ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client())
 {
 proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(new Uri("net.tcp://localhost:71/Service"));
 proxy.Endpoint.Binding = new NetTcpBinding();
 proxy.Endpoint.Contract.ContractType = typeof(WCFAppTest.Service.IService1);
 Console.WriteLine("Net.TCP: 1 + 1 is " + proxy.GetSum(1, 1));
 proxy.Close();
 }
  
 using (ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client())
 {
 proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(new Uri("http://localhost:72/Service"));
 proxy.Endpoint.Binding = new WSHttpBinding();
 proxy.Endpoint.Contract.ContractType = typeof(WCFAppTest.Service.IService1);
 Console.WriteLine("HTTP: 2 + 2 is " + proxy.GetSum(2, 2));
 proxy.Close();
 }
  
 Console.ReadLine();
ServiceReference1 is a reference You have to add to Your project. Simply do it by clicking RMB on the “Service Reference” node and the “Add Service Reference…”. Next step is to click “Discover” and when Your svc file is found click OK.

OK, now You can run first the service console app and then the test console app and You should see the results like this:

 

HTTP COULD NOT REGISTER URL HTTP://+8080/MyServce

HTTP could not register URL http://+:8080/MyService/. Another application has already registered this URL

Problem :
While running a WCF solution (including a service project and a service host) the following error can occur:
“HTTP could not register URL http://+:8080/MyService/. Another application has already registered this URL with HTTP.SYS.”
I faced this problem when I tried to run some of the exercises while doing my labs in school.
Impact :
Application could not run in normal mode. And application gives an exception that ‘HTTP could not register URL http://+:8080/MyService/’.
Solution:
The exception itself has offered helping hands on how to register Http Namespace by saying(see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
I ran the following command under the .Net Command Prompt.
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>netsh http add urlacl url=http://+:8080/CalculatorService/Meta/ user=\Everyone
Url reservation add failed, Error: 5
The requested operation requires elevation (Run as administrator).

I didn’t get through it yet, because by default the .Net command prompt doesn’t have access to register the Http Namespaces.
I reran the .Net command prompt in Administrator mode (Right Click the .Net Command Prompt in Start Menu, and select Run as Administrator).
The same command worked well in Administrator mode.
C:\Windows\system32>netsh http add urlacl url=http://+:8080/MyService/ user=\Everyone
URL reservation successfully added

Another simple alternate to using netsh http command is to run the Visual Studio itself in Administrator mode.
Note: netsh is useful for a verity of work. Refer the following pages for more detail.
Netsh Command Reference for HTTP
Netsh Command Reference
 

Saturday, 29 September 2012

linq to sharepoint 2010

    With that done – all we need to do is import it to one of our projects and use it!

    Visual Studio 2010 – Let’s create a sample Web Part that utilizes LINQ to SharePoint

    In this sample I will create a simple Web Part that will use LINQ to SharePoint syntax to fetch some information from the Announcements list. A basic sample I use in my training classes as well, and should be fairly easy to grasp!
    1. Create a new project (I’m going to create a new Visual Web Part project)
    2. Import your DataContext-file by choosing your Project -> Add -> Existing Item:
    3. Specify your file (mine is called MyEntities.cs):
    4. Make sure it’s properly placed in your project structure – then we’re good to go:
      image  
    Alright – that’s easy enough. Thus far we have created an entity file using SPMetal.exe and now we have successfully imported it into our project.

    Add proper references

    Now in order to use LINQ to SharePoint, you also need to reference the Microsoft.SharePoint.Linq assembly. Point to references, right-click and choose "Add Reference" and select the Microsoft.SharePoint.Linq.dll file:
    image  
    In your code, reference the assemblies:
    image  

    Ready to code?

    What you should’ve done up until now is this:
    1. Generate your entities using the SPMetal.exe tool
    2. Reference the newly created file from your SharePoint project
    3. Make sure you’re using the proper references for System.Linq and Microsoft.SharePoint.Linq
    4. Be ready to code :-)

    Code!

    In my example I will have a Visual Web Part that will use LINQ to SharePoint to fetch all Announcements from my Announcement-list and work with those results. If you want to see the entire project, look at the bottom of this article where you can download it.
    image 

    IntelliSense!

    While you code your queries using LINQ to SharePoint, you will now have access to IntelliSense, which you did not have with CAML queries:
    image  
    I’m going to leave it at that – very (very) easy to get started with LINQ to SharePoint, and all you really need to know is to start using the SPMetal tool to generate your entity classes and hook’em up with Visual Studio to start coding.
    The two following questions are quite popular in my SharePoint 2010 developer training classes, so I ought to answer them right here.
    1. "What does LINQ to SharePoint really do?"
    2. "Can I see the CAML query generated by the LINQ to SharePoint query?"
    The answer is simple: Yes, you can see the results of your generated LINQ query by using the Log property of your DataContext object.

    What CAML lied behind my LINQ to SharePoint query?

    In order to fetch the CAML query that lies behind your LINQ query, all you need is to work with the .Log object of your DataContext.
    See this simple example, which simply outputs the CAML query to an XML-file for easy reading:
    image  
    This will essentially generate the following content in the file C:MyEntitiesDataContextQuery.xml:
    image  
    As you can see, the LINQ to SharePoint query is automatically turned into a CAML Query.

    Summary

    Yep, all you need is the .Log property to fetch the CAML query from your LINQ statement. In my sample I’m outputting it to a file called C:MyEntitiesDataContextQuery.xml.
    You could of course output it in any other way you want – not just a physical file on the file system. The Log property is of the type TextWriter.

    No comments:


    Post a Comment








    -->

    .bind(event type, event handler)




    Thursday, 6 September 2012


    SP 2010: Getting started with LINQ to SharePoint in SharePoint 2010

    Introduction

    In SharePoint 2010 you now have the ability to use LINQ syntax to fetch items from your lists instead of using the "traditional" approach of CAML queries. (Including SPSiteDataQuery and SPQuery objects)
    In this article I will give you a brief introduction to how you can get started using LINQ queries in SharePoint, also known as LINQ to SharePoint.

    Basics of LINQ?

    As a prerequisite to this article, I’m going to imply that you know what LINQ is and how to write basic LINQ queries in any .NET application already. I’m not going to dive into the details about LINQ or the syntax itself here – please see MSDN for that!

    LINQ to SharePoint!

    In order to work with LINQ in SharePoint 2010, we need use a tool called SPMetal.exe which resides in the 14bin folder. This tool is used to generate some entity classes which Visual Studio 2010 can use to get IntelliSense, and allows for LINQ-based queries to be performed on your lists.
    Noteworthy:
    • LINQ to SharePoint queries are translated to proper CAML queries
    • CAML queries are in turn later translated to SQL queries

    SPMetal.exe

    Using the tool called SPMetal, we generate our entity-classes that are needed to perform these object oriented queries toward our SharePoint server.
    These are the required steps to get hooked up:
      image
    1. Launch a cmd-window and navigate to C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14bin
      image
    2. Run the following command to utilize the SPMetal.exe tool with the following syntax:
      1. SPMetal.exe /web:http://yoursite /code:C:YourEntityFile.cs
      2. Example:
        image
  1. Now navigate to C: (or wherever you chose to output your file) and make sure the file has been generated:
    image
  2. Open up the file and take a look at the content that SPMetal now have provided us with:

    Note that the class name is now MyEntitiesDataContext. It’s based on the name you specify as your code file in the SPMetal.exe command line tool. If you were to use /code:C:Awesome.cs instead, it would generate a class called AwesomeDataContext.
    1. With that done – all we need to do is import it to one of our projects and use it!

      Visual Studio 2010 – Let’s create a sample Web Part that utilizes LINQ to SharePoint

      In this sample I will create a simple Web Part that will use LINQ to SharePoint syntax to fetch some information from the Announcements list. A basic sample I use in my training classes as well, and should be fairly easy to grasp!
      1. Create a new project (I’m going to create a new Visual Web Part project)
      2. Import your DataContext-file by choosing your Project -> Add -> Existing Item:
      3. Specify your file (mine is called MyEntities.cs):
      4. Make sure it’s properly placed in your project structure – then we’re good to go:
        image
      Alright – that’s easy enough. Thus far we have created an entity file using SPMetal.exe and now we have successfully imported it into our project.

      Add proper references

      Now in order to use LINQ to SharePoint, you also need to reference the Microsoft.SharePoint.Linq assembly. Point to references, right-click and choose "Add Reference" and select the Microsoft.SharePoint.Linq.dll file:
      image
      In your code, reference the assemblies:
      image

      Ready to code?

      What you should’ve done up until now is this:
      1. Generate your entities using the SPMetal.exe tool
      2. Reference the newly created file from your SharePoint project
      3. Make sure you’re using the proper references for System.Linq and Microsoft.SharePoint.Linq
      4. Be ready to code :-)

      Code!

      In my example I will have a Visual Web Part that will use LINQ to SharePoint to fetch all Announcements from my Announcement-list and work with those results. If you want to see the entire project, look at the bottom of this article where you can download it.
      image

      IntelliSense!

      While you code your queries using LINQ to SharePoint, you will now have access to IntelliSense, which you did not have with CAML queries:
      image
      I’m going to leave it at that – very (very) easy to get started with LINQ to SharePoint, and all you really need to know is to start using the SPMetal tool to generate your entity classes and hook’em up with Visual Studio to start coding.


      The two following questions are quite popular in my SharePoint 2010 developer training classes, so I ought to answer them right here.
      1. "What does LINQ to SharePoint really do?"
      2. "Can I see the CAML query generated by the LINQ to SharePoint query?"
      The answer is simple: Yes, you can see the results of your generated LINQ query by using the Log property of your DataContext object.

      What CAML lied behind my LINQ to SharePoint query?

      In order to fetch the CAML query that lies behind your LINQ query, all you need is to work with the .Log object of your DataContext.
      See this simple example, which simply outputs the CAML query to an XML-file for easy reading:
      image
      This will essentially generate the following content in the file C:MyEntitiesDataContextQuery.xml:
      image
      As you can see, the LINQ to SharePoint query is automatically turned into a CAML Query.

      Summary

      Yep, all you need is the .Log property to fetch the CAML query from your LINQ statement. In my sample I’m outputting it to a file called C:MyEntitiesDataContextQuery.xml.
      You could of course output it in any other way you want – not just a physical file on the file system. The Log property is of the type TextWriter.

      No comments:


      Post a Comment








      -->



      Thursday, 6 September 2012


      SP 2010: Getting started with LINQ to SharePoint in SharePoint 2010

      Introduction

      In SharePoint 2010 you now have the ability to use LINQ syntax to fetch items from your lists instead of using the "traditional" approach of CAML queries. (Including SPSiteDataQuery and SPQuery objects)
      In this article I will give you a brief introduction to how you can get started using LINQ queries in SharePoint, also known as LINQ to SharePoint.

      Basics of LINQ?

      As a prerequisite to this article, I’m going to imply that you know what LINQ is and how to write basic LINQ queries in any .NET application already. I’m not going to dive into the details about LINQ or the syntax itself here – please see MSDN for that!

      LINQ to SharePoint!

      In order to work with LINQ in SharePoint 2010, we need use a tool called SPMetal.exe which resides in the 14bin folder. This tool is used to generate some entity classes which Visual Studio 2010 can use to get IntelliSense, and allows for LINQ-based queries to be performed on your lists.
      Noteworthy:
      • LINQ to SharePoint queries are translated to proper CAML queries
      • CAML queries are in turn later translated to SQL queries

      SPMetal.exe

      Using the tool called SPMetal, we generate our entity-classes that are needed to perform these object oriented queries toward our SharePoint server.
      These are the required steps to get hooked up:
        image
      1. Launch a cmd-window and navigate to C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14bin
        image
      2. Run the following command to utilize the SPMetal.exe tool with the following syntax:
        1. SPMetal.exe /web:http://yoursite /code:C:YourEntityFile.cs
        2. Example:
          image
    2. Now navigate to C: (or wherever you chose to output your file) and make sure the file has been generated:
      image
    3. Open up the file and take a look at the content that SPMetal now have provided us with:

      Note that the class name is now MyEntitiesDataContext. It’s based on the name you specify as your code file in the SPMetal.exe command line tool. If you were to use /code:C:Awesome.cs instead, it would generate a class called AwesomeDataContext.
      1. With that done – all we need to do is import it to one of our projects and use it!

        Visual Studio 2010 – Let’s create a sample Web Part that utilizes LINQ to SharePoint

        In this sample I will create a simple Web Part that will use LINQ to SharePoint syntax to fetch some information from the Announcements list. A basic sample I use in my training classes as well, and should be fairly easy to grasp!
        1. Create a new project (I’m going to create a new Visual Web Part project)
        2. Import your DataContext-file by choosing your Project -> Add -> Existing Item:
        3. Specify your file (mine is called MyEntities.cs):
        4. Make sure it’s properly placed in your project structure – then we’re good to go:
          image
        Alright – that’s easy enough. Thus far we have created an entity file using SPMetal.exe and now we have successfully imported it into our project.

        Add proper references

        Now in order to use LINQ to SharePoint, you also need to reference the Microsoft.SharePoint.Linq assembly. Point to references, right-click and choose "Add Reference" and select the Microsoft.SharePoint.Linq.dll file:
        image
        In your code, reference the assemblies:
        image

        Ready to code?

        What you should’ve done up until now is this:
        1. Generate your entities using the SPMetal.exe tool
        2. Reference the newly created file from your SharePoint project
        3. Make sure you’re using the proper references for System.Linq and Microsoft.SharePoint.Linq
        4. Be ready to code :-)

        Code!

        In my example I will have a Visual Web Part that will use LINQ to SharePoint to fetch all Announcements from my Announcement-list and work with those results. If you want to see the entire project, look at the bottom of this article where you can download it.
        image

        IntelliSense!

        While you code your queries using LINQ to SharePoint, you will now have access to IntelliSense, which you did not have with CAML queries:
        image
        I’m going to leave it at that – very (very) easy to get started with LINQ to SharePoint, and all you really need to know is to start using the SPMetal tool to generate your entity classes and hook’em up with Visual Studio to start coding.


        The two following questions are quite popular in my SharePoint 2010 developer training classes, so I ought to answer them right here.
        1. "What does LINQ to SharePoint really do?"
        2. "Can I see the CAML query generated by the LINQ to SharePoint query?"
        The answer is simple: Yes, you can see the results of your generated LINQ query by using the Log property of your DataContext object.

        What CAML lied behind my LINQ to SharePoint query?

        In order to fetch the CAML query that lies behind your LINQ query, all you need is to work with the .Log object of your DataContext.
        See this simple example, which simply outputs the CAML query to an XML-file for easy reading:
        image
        This will essentially generate the following content in the file C:MyEntitiesDataContextQuery.xml:
        image
        As you can see, the LINQ to SharePoint query is automatically turned into a CAML Query.

        Summary

        Yep, all you need is the .Log property to fetch the CAML query from your LINQ statement. In my sample I’m outputting it to a file called C:MyEntitiesDataContextQuery.xml.
        You could of course output it in any other way you want – not just a physical file on the file system. The Log property is of the type TextWriter.

        No comments:


        Post a Comment







        -->




        Thursday, 6 September 2012


        SP 2010: Getting started with LINQ to SharePoint in SharePoint 2010

        Introduction

        In SharePoint 2010 you now have the ability to use LINQ syntax to fetch items from your lists instead of using the "traditional" approach of CAML queries. (Including SPSiteDataQuery and SPQuery objects)
        In this article I will give you a brief introduction to how you can get started using LINQ queries in SharePoint, also known as LINQ to SharePoint.

        Basics of LINQ?

        As a prerequisite to this article, I’m going to imply that you know what LINQ is and how to write basic LINQ queries in any .NET application already. I’m not going to dive into the details about LINQ or the syntax itself here – please see MSDN for that!

        LINQ to SharePoint!

        In order to work with LINQ in SharePoint 2010, we need use a tool called SPMetal.exe which resides in the 14bin folder. This tool is used to generate some entity classes which Visual Studio 2010 can use to get IntelliSense, and allows for LINQ-based queries to be performed on your lists.
        Noteworthy:
        • LINQ to SharePoint queries are translated to proper CAML queries
        • CAML queries are in turn later translated to SQL queries

        SPMetal.exe

        Using the tool called SPMetal, we generate our entity-classes that are needed to perform these object oriented queries toward our SharePoint server.
        These are the required steps to get hooked up:
          image
        1. Launch a cmd-window and navigate to C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14bin
          image
        2. Run the following command to utilize the SPMetal.exe tool with the following syntax:
          1. SPMetal.exe /web:http://yoursite /code:C:YourEntityFile.cs
          2. Example:
            image
      2. Now navigate to C: (or wherever you chose to output your file) and make sure the file has been generated:
        image
      3. Open up the file and take a look at the content that SPMetal now have provided us with:

        Note that the class name is now MyEntitiesDataContext. It’s based on the name you specify as your code file in the SPMetal.exe command line tool. If you were to use /code:C:Awesome.cs instead, it would generate a class called AwesomeDataContext.
        1. With that done – all we need to do is import it to one of our projects and use it!

          Visual Studio 2010 – Let’s create a sample Web Part that utilizes LINQ to SharePoint

          In this sample I will create a simple Web Part that will use LINQ to SharePoint syntax to fetch some information from the Announcements list. A basic sample I use in my training classes as well, and should be fairly easy to grasp!
          1. Create a new project (I’m going to create a new Visual Web Part project)
          2. Import your DataContext-file by choosing your Project -> Add -> Existing Item:
          3. Specify your file (mine is called MyEntities.cs):
          4. Make sure it’s properly placed in your project structure – then we’re good to go:
            image
          Alright – that’s easy enough. Thus far we have created an entity file using SPMetal.exe and now we have successfully imported it into our project.

          Add proper references

          Now in order to use LINQ to SharePoint, you also need to reference the Microsoft.SharePoint.Linq assembly. Point to references, right-click and choose "Add Reference" and select the Microsoft.SharePoint.Linq.dll file:
          image
          In your code, reference the assemblies:
          image

          Ready to code?

          What you should’ve done up until now is this:
          1. Generate your entities using the SPMetal.exe tool
          2. Reference the newly created file from your SharePoint project
          3. Make sure you’re using the proper references for System.Linq and Microsoft.SharePoint.Linq
          4. Be ready to code :-)

          Code!

          In my example I will have a Visual Web Part that will use LINQ to SharePoint to fetch all Announcements from my Announcement-list and work with those results. If you want to see the entire project, look at the bottom of this article where you can download it.
          image

          IntelliSense!

          While you code your queries using LINQ to SharePoint, you will now have access to IntelliSense, which you did not have with CAML queries:
          image
          I’m going to leave it at that – very (very) easy to get started with LINQ to SharePoint, and all you really need to know is to start using the SPMetal tool to generate your entity classes and hook’em up with Visual Studio to start coding.


          The two following questions are quite popular in my SharePoint 2010 developer training classes, so I ought to answer them right here.
          1. "What does LINQ to SharePoint really do?"
          2. "Can I see the CAML query generated by the LINQ to SharePoint query?"
          The answer is simple: Yes, you can see the results of your generated LINQ query by using the Log property of your DataContext object.

          What CAML lied behind my LINQ to SharePoint query?

          In order to fetch the CAML query that lies behind your LINQ query, all you need is to work with the .Log object of your DataContext.
          See this simple example, which simply outputs the CAML query to an XML-file for easy reading:
          image
          This will essentially generate the following content in the file C:MyEntitiesDataContextQuery.xml:
          image
          As you can see, the LINQ to SharePoint query is automatically turned into a CAML Query.

          Summary

          Yep, all you need is the .Log property to fetch the CAML query from your LINQ statement. In my sample I’m outputting it to a file called C:MyEntitiesDataContextQuery.xml.
          You could of course output it in any other way you want – not just a physical file on the file system. The Log property is of the type TextWriter.

          No comments:


          Post a Comment








          -->