30 July 2011

Optional Parameters and Named Arguments in Framework 4

This time I will demonstrate another nice feature in the framework 4
C # 4.0 now supports using Optional Parameters with Methods, Constructors, and Indexers.

Earlier versions of the framework, create a mountain default variable is actually needs to create two methods.

 private static double GetPriceIncludesVAT(double p, double vat)
 {
     return p * vat;
 }
 private static double GetPriceIncludesVAT(double p)
 {
     return GetPriceIncludesVAT(p, 16.5);
 }


Today, with the framework 4, C #, we can give a default (like VB) to methods.


Another nice thing we got, it Named Arguments.
So that the code will organized - we can be the names in a methods call.
Look for the following example:

 double priceWith15VAT = GetPriceIncludesVAT(vat: 15, p: 50);

Here is the Code:

using System;
namespace OptionalParm
{
    class Program
    {
        static void Main(string[] args)
        {
            double priceDefult = GetPriceIncludesVAT(50.0);
            Console.WriteLine("Price with 16.5% VAT (defult value) of 50 is: " + priceDefult);
            double priceWith17VAT = GetPriceIncludesVAT(50.0,17);
            Console.WriteLine("Price with 17% VAT of 50 is: " + priceWith17VAT);
            double priceWith15VAT = GetPriceIncludesVAT(vat: 15, p: 50);
            Console.WriteLine("Price with 15% VAT of 50 is: " + priceWith15VAT);
            Console.ReadKey();
        }
        private static double GetPriceIncludesVAT(double p,double vat = 16.5)
        {
            return p * vat;
        }
     }
} 

The Result:

Other examples of  framework 4:
Yours,
Roi

16 July 2011

Get a SharePoint List of Client Object Model in SharePoint 2010

One serious benefits provides us with the code in SP2010 is the  Client Object Model
Client Object Model gives us the client-side API.
Example:
Opens a console project


We will Add Reference the DLL to the following: Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll form 14Hive\Isapi folder.


Now type the following code:

using System;
using
System.Linq;
using
MyClientOM = Microsoft.SharePoint.Client;
namespace
TestClientOM
{
    class Program
   
{
       
static void Main(string[] args)
        {
           
string myUrl = "http://mysite.com/";
           
using (MyClientOM.ClientContext ctx = new MyClientOM.ClientContext(myUrl))
           
{
                MyClientOM.Web site = ctx.Web;


                ctx.Load(site);
                ctx.Load(site.Lists);
                ctx.Load(site, x => x.Lists.Where(l => l.Title != null));
                ctx.ExecuteQuery();
                Console.WriteLine("Lists:");
               
foreach (MyClientOM.List list in site.Lists)
                {

                    Console.WriteLine(list.Title);
                }
                Console.ReadKey();
            }
       
}
   
}
}

The code gave us remote access to the site myUrl through code. The code Brought us all the lists on the site.



Thanks
Roi

04 July 2011

Get Alternate Access Mappings by code

At the time I explained to you what problems occur when the wrong set of Alternate Access Mappings.
This time I will show you how to get the Alternate Access Mappings by code.

The following function returns an array of links for Current Web Application - Alternate Access Mappings.

/// <summary>
/// Create a list with the alternate access mapping urls
/// </summary>
/// <returns></returns>
private List<Uri> GetCurrentAlternateAccessMappingsUrls()
{
    List<Uri> alternateUrls = null;
    // Make sure anonymous users can access the web application properties
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        alternateUrls = new List<Uri>();
        // Retrieve the WebApplication object
        SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
        // Get Alternate Url
        foreach (SPAlternateUrl alternateUrl in webApplication.AlternateUrls)
        {
            Uri url = new Uri(alternateUrl.IncomingUrl);
            // Fill the List
            alternateUrls.Add(url);
        }
    });
    // Return the list with the urls              
    return alternateUrls;      
}

Nice .. :)