October 22, 2012

Nuget for Visual Studio

Nuget​ is a package manager for visual studio. It allows developers to setup third party libraries in the project easily. For example if you are working on a console application to migrate documents and want to use log4net library. You can just search for log4net in package manager gallery and add reference to it. All web.config/app.config and references will be auomatically updated by nuget.
Install nuget to your visual studio and start learning new libraries now!


If you have any questions you can reach out our SharePoint Consulting team here.

log4net - logging makes easy

Use case:
You are working on some task which requires logging to event viewer, file, console or combination of such mediums.
Why?
Most of the time you have to write same string to console and file with two different lines of code. That will cause the code look ugly and while changing messages, developer has to make sure to update both lines. Additional errors may occure due to the mistakes.
Prerequisites:
Install nuget for visual studio.
How?
Right click on your project and click on library package manager. Search for log4net and choose appropriate version as per your visual studio application. Click on install.
What are different mediums where I can log? 
  • MS SQL Server
  • MS Access
  • Oracle 9i
  • Oracle 8i
  • IBM DB2
  • SQLite
  • Asp.Net Trace
  • Console
  • EventLog
  • File
  • SMTP
  • much more
Example:
See vss_br/bms/trunk/bms/bmsmigration project in vss for rerence implementation in console application for logging in console and file.
References:
  1. Code project tutorial
  2. Configuration Examples
  3. Log4net config example to Log into console and file both
  4. Official Home page


If you have any questions you can reach out our SharePoint Consulting team here.

How to use ASMX services in visual web part development

Use case:
Visual web part needs to fetch data from aspx web service. WSDL url is provided.
Service reference vs Code generation:
There are two ways to add reference to asmx services. You can directly right click the web part project and click on add service reference, click on advance and then choose asmx service reference.
Adding service reference will hard code the service url. So it is suggested that we use WSDL.exe available in .net framework tools to generate a client proxy.
How to use WSDL.exe to generate service proxy?
  1. Go to .net command prompt
  2. type wsdl /language:csharp /out:c\csfilename.cs
This will automatically generate the cs file provided in out paramter. Add the class to your web part folder and adjust the namespace.
Further Considerations:
  1. Url of the service should not be hard coded, it should be coming from web part property.
  2. To imlpement this, add a custom property in web part to store service url
  3. Add another paramterized constructor to the first class. See example below:
    Default constructor:
     public Spotlight()
            {
                this.Url = "web service url";
            }
    New constructor:
     public Spotlight(string _url)
    {
      this.Url = _url;
    }
  4. Generally the first class in wsdl generated proxy cs will be the one where you need to add the new constructor.
  5. You can confirm this by looking at the first class and it will be inheriting from
          "System.Web.Services.Protocols.SoapHttpClientProtocol" class.
  6. When web part property for service url is blank, use default constructor
  7. When web part property for service url is not blank use parameterized constructor.
  8. When your service will change, you have to repeat the process to generate proxy and add additional parameterized constructor
Other best practices:
  1. Always use proper error handling to make sure exceptions are not visible to end users
  2. Always show appropriate message in case of no data to users
  3. Always show appropriate message in case of error getting data from service
  4. Always use best practice to parse xml data.
  5. If possible use xml deserializers to convert xml data into List
Conclusion:
We find it very useful to use WSDL.exe to generate asmx proxy and using constructor injection to initialize the service url from web part property.

If you have any questions you can reach out our SharePoint Consulting team here.

SPLongOperation

Use Case:
SPLongOperation class Sets the Web page image to the image used by the server to indicate a lengthy operation (typically, an animated image with associated text).
So, when you are starting a workflow, or doing some server side processing that can take long period than asp.net request execution timeout period, you can use this class to do such operations.
Usage:
   1:  SPLongOperation.BeginOperation beginOperation = null;
   2:                  if (beginOperation == null)
   3:                  {
   4:                      beginOperation = delegate(SPLongOperation longOperation)
   5:                      {
   6:                          // Long running code here ...
   7:                          longOperation.End(this.workflowList.DefaultViewUrl, SPRedirectFlags.UseSource, HttpContext.Current, "");
   8:                      };
   9:                  }
  10:                  SPLongOperation.Begin(beginOperation);
 
 
Notes:
You need to import System.Threading. At the end of your operation, generally you do longOperation.End() and page will be redirected to the specified url. If you are doing it in sharepoint dialog, it will automatically close dialog and redirect parent page.

If you have any questions you can reach out our SharePoint Consulting team here.

Printing InfoPath Forms

Print option is normally not available as an option on Ribbon in InfoPath templates. So if one wants an option to “Print” InfoPath form then they can use the following steps below. Before starting off the InfoPath should be published as a Content type and a Page with InfoPath form Web Part must be created.
  • Open the Display Form web part page (make sure to select the correct content types if you have more than one....). You can do this by clicking Form Web Parts > (Content Type if you have several) Display Form
  • Edit the InfoPath Form Web Part. Set the Chrome to include just the Title.
  • Add a Content Editor Web part to the page. Set it to Hidden.
  • Add the JS from below link in the HTML source of the Content Editor Web part.
    https://www.nothingbutsharepoint.com/sites/eusp/Pages/jquery-for-everyone-print-any-web-part.aspx
  • Save the page.
You can now click the little icon (or you can modify the JS to insert a button to say 'Print Form') and the Print Preview of the Info Path web part gets opened, along with popup for setting printer options.
In the above JS script, the function “printWebPart” can be modified and replaced by the below script function

  printWebPart(tagid)
        {
            if (tagid)
                {
                    this.print();
                }
        }
 

This will directly open up the popup for setting printer options without showing the Print Preview.

If you have any questions you can reach out our SharePoint Consulting team here.