tag:blogger.com,1999:blog-91648375145314506192024-03-14T01:50:02.155-07:00.Net GoodiesCode snippets and technology news that come across my daily life.A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.comBlogger156125tag:blogger.com,1999:blog-9164837514531450619.post-43296254223658875582011-06-28T23:09:00.001-07:002011-06-28T23:09:34.741-07:00Service Pack 1 for SharePoint 2010 Products is Now Available for Download<p>Service Pack 1 for SharePoint 2010 Products is Now Available for <a href="http://technet.microsoft.com/office/ee748587.aspx">Download</a></p> <p>Service Pack 1 includes stability, performance, and security enhancements that are a direct result of your feedback. </p> <p>It is strongly recommended to install the <a href="http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2536599">June 2011 Cumulative Update </a>immediately after the installation of Service Pack 1. The June Cumulative Update includes several important security and bug fixes that are not included Service Pack 1. </p> <p>Prior to installing Service Pack 1 you should carefully read the known issues and release notes at </p> <p><a href="http://support.microsoft.com/kb/2532126">http://support.microsoft.com/kb/2532126</a></p> <p><a href="http://sharepoint.microsoft.com/blog/Pages/BlogPost.aspx?pID=984">Service Pack 1 for SharePoint 2010 Products is Now Available for Download</a></p> <p>by <img border="0" alt="alt" src="http://sharepoint.microsoft.com/_layouts/images/blank.gif" width="12" height="12" />sharepointblog@live.com on  6/28/2011 7:02 AM</p> <p>Category:  <a href="http://sharepoint.microsoft.com/blog/Pages/CategoriesCategory.aspx?CategoryId=157&Name=SharePoint%202010">SharePoint 2010</a></p> <p>Service Pack 1 for SharePoint 2010 Products is Now Available for <a href="http://technet.microsoft.com/office/ee748587.aspx">Download</a></p> <p>Service Pack 1 includes stability, performance, and security enhancements that are a direct result of your feedback. </p> <p><strong>IMPORTANT NOTE</strong></p> <p>It is strongly recommended to install the <a href="http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2536599">June 2011 Cumulative Update </a>immediately after the installation of Service Pack 1. The June Cumulative Update includes several important security and bug fixes that are not included Service Pack 1. </p> <p><strong>Installing Service Pack 1 </strong></p> <p>Prior to installing Service Pack 1 you should carefully read the known issues and release notes at </p> <p><a href="http://support.microsoft.com/kb/2532126">http://support.microsoft.com/kb/2532126</a></p> <p>Service Pack 1 includes all fixes released through April 2011 so it can be installed directly to RTM builds of SharePoint 2010 Products, or any prior Cumulative Update. </p> <p>Install the service packs in the following order on every server in the farm. </p> <blockquote> <p>1. <a href="http://www.microsoft.com/download/en/details.aspx?id=26640">Service Pack 1 for SharePoint Foundation 2010</a></p> <p>2. <a href="http://www.microsoft.com/download/en/details.aspx?id=26629">Service Pack 1 for SharePoint Foundation 2010 Language Pack (if applicable)</a></p> <p>3. <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=B9FCDC42-EEA4-4C08-9169-A9A73E55B8D4">Service Pack 1 for SharePoint Server 2010 </a></p> <p>4. <a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=26621">Service Pack 1 for SharePoint Server 2010 Language Pack (if applicable)</a></p> </blockquote> <p>The SharePoint 2010 Products Configuration Wizard or "psconfig –cmd upgrade –inplace b2b -wait” should be run once on every server in the farm following the final update installed. </p> <p>The version of content databases will be 14.0.6029.1000 after successfully installation. For more in-depth guidance for the update process, we recommend reviewing the following articles. These articles provide a correct way to deploy updates and identify known issues (and resolutions). </p> <p><a href="http://technet.microsoft.com/library/ff806335%28office.14%29.aspx">Prepare to deploy a software update for SharePoint Foundation 2010</a></p> <p><a href="http://technet.microsoft.com/library/ff806325%28office.14%29.aspx">Install a software update for SharePoint Foundation 2010</a></p> <p><a href="http://technet.microsoft.com/library/ff806331%28office.14%29.aspx">Prepare to deploy a software update for SharePoint Server 2010</a></p> <p><a href="http://technet.microsoft.com/library/ff806338%28office.14%29.aspx">Install a software update for SharePoint Server 2010</a></p> <p></p> <p><strong>Frequently Asked Questions </strong></p> <p>Q: Can I install Service Pack 1 on RTM builds of SharePoint 2010 Products? </p> <p>A: Yes, Service Pack 1 can be installed directly on RTM builds; however, we suggest you install Service Pack 1 then apply the June 2011 Cumulative Update. </p> <p>Q: Do I need to run psconfig after the install of every package? </p> <p>A: No, apply all of the available packages then run psconfig - the database will only be updated once, to the newest version. </p> <p>Q: Do I need to run psconfig on every machine in the farm? </p> <p>A: Yes. Although database is already updated, the binaries on each server need to be set and permissioned using psconfig. </p> <p>Q: Will there be a slipstream build including Service Pack 1 available for download? </p> <p>A: At this time a slipstream build including Service Pack 1 is not available. </p> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-47523815290718981072011-05-18T00:06:00.000-07:002011-05-18T00:06:33.451-07:00Serialize Multiobject ArrayList to XMLThis is a code snippet which shows how to Serialize an ArrayList to XML. ArrayList contains objects of different types.<br />
<br />
<pre class="code"><span style="color: blue;">using </span>System.Collections;
<span style="color: blue;">using </span>System.IO;
<span style="color: blue;">using </span>System.Xml;
<span style="color: blue;">using </span>System.Xml.Serialization;
<span style="color: blue;">namespace </span>ConsoleApplication1
{
<span style="color: blue;">class </span><span style="color: #2b91af;">Program
</span>{
<span style="color: blue;">static void </span>Main(<span style="color: blue;">string</span>[] args)
{
<span style="color: #2b91af;">AListWrapper </span>wrapper = <span style="color: blue;">new </span><span style="color: #2b91af;">AListWrapper</span>();
<span style="color: #2b91af;">XmlSerializer </span>mySerializer = <span style="color: blue;">new </span><span style="color: #2b91af;">XmlSerializer</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">AListWrapper</span>));
<span style="color: #2b91af;">StreamWriter </span>myWriter = <span style="color: blue;">new </span><span style="color: #2b91af;">StreamWriter</span>(<span style="color: #a31515;">"c:\\myFileName.xml"</span>);
mySerializer.Serialize(myWriter, wrapper);
myWriter.Close();
}
}
<span style="color: blue;">public class </span><span style="color: #2b91af;">Animal
</span>{
<span style="color: blue;">public string </span>Type { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
<span style="color: blue;">public int </span>Age { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
}
<span style="color: blue;">public class </span><span style="color: #2b91af;">Employee
</span>{
<span style="color: blue;">public string </span>Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
<span style="color: blue;">public double </span>Salary { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
<span style="color: blue;">public string </span>Address { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
}
[<span style="color: #2b91af;">XmlRoot</span>(<span style="color: #a31515;">"ArrayList"</span>)]
<span style="color: blue;">public class </span><span style="color: #2b91af;">AListWrapper
</span>{
[<span style="color: #2b91af;">XmlElement</span>(Type = <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Employee</span>)),
<span style="color: #2b91af;">XmlElement</span>(Type = <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Animal</span>))]
<span style="color: blue;">public </span><span style="color: #2b91af;">ArrayList </span>list = <span style="color: blue;">new </span><span style="color: #2b91af;">ArrayList</span>();
<span style="color: blue;">public </span>AListWrapper()
{
<span style="color: #2b91af;">Animal </span>animal = <span style="color: blue;">new </span><span style="color: #2b91af;">Animal</span>()
{
Age = 1,
Type = <span style="color: #a31515;">"Dog"
</span>};
<span style="color: #2b91af;">Employee </span>emp = <span style="color: blue;">new </span><span style="color: #2b91af;">Employee</span>()
{
Address = <span style="color: #a31515;">"Address"</span>,
Name = <span style="color: #a31515;">"SomeName"</span>,
Salary = 2000.50
};
list.Add(animal);
list.Add(emp);
}
}
}</pre><br />
<a href="http://11011.net/software/vspaste"></a>A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com2tag:blogger.com,1999:blog-9164837514531450619.post-39564684271794560992011-05-04T00:39:00.000-07:002011-05-04T00:39:32.226-07:00jQuery 1.6 Released<a href="http://www.amazon.com/Sams-Teach-Yourself-jQuery-Hours/dp/0672335565?ie=UTF8&tag=netgood-20&link_code=bil&camp=213689&creative=392969" imageanchor="1" target="_blank"><img alt="Sams Teach Yourself jQuery in 24 Hours" src="http://ws.amazon.com/widgets/q?MarketPlace=US&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=_SL160_&ASIN=0672335565&tag=netgood-20" /></a><img alt="" border="0" height="1" src="http://www.assoc-amazon.com/e/ir?t=netgood-20&l=bil&camp=213689&creative=392969&o=1&a=0672335565" style="border: none !important; margin: 0px !important; padding: 0px !important;" width="1" /><br />
<b>jQuery 1.6</b> is now available for download!<br />
<br />
jQuery CDN:<br />
<ul><li><a href="http://code.jquery.com/jquery-1.6.js" target="_blank">http://code.jquery.com/jquery-1.6.js</a> </li>
<li><a href="http://code.jquery.com/jquery-1.6.min.js" target="_blank">http://code.jquery.com/jquery-1.6.min.js</a> </li>
</ul>Microsoft Ajax CDN:<br />
<ul><li><a href="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.min.js" target="_blank" title="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.min.js">http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.min.js</a></li>
</ul>You can read more about this on the following link: <a href="http://blog.jquery.com/2011/05/03/jquery-16-released/" target="_blank" title="http://blog.jquery.com/2011/05/03/jquery-16-released/">http://blog.jquery.com/2011/05/03/jquery-16-released/</a>A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-80287757669300593412011-05-02T00:38:00.001-07:002011-05-02T00:47:07.944-07:00SharePoint 2010 Word Automation Services<a href="http://www.amazon.com/Professional-SharePoint-2010-Administration-Klindt/dp/0470533331?ie=UTF8&tag=netgood-20&link_code=bil&camp=213689&creative=392969" target="_blank" imageanchor="1"><img alt="Professional SharePoint 2010 Administration" src="http://ws.amazon.com/widgets/q?MarketPlace=US&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=_SL160_&ASIN=0470533331&tag=netgood-20" /></a><img style="border-bottom-style: none !important; padding-bottom: 0px !important; margin: 0px; border-left-style: none !important; padding-left: 0px !important; padding-right: 0px !important; border-top-style: none !important; border-right-style: none !important; padding-top: 0px !important" border="0" alt="alt" src="http://www.assoc-amazon.com/e/ir?t=netgood-20&l=bil&camp=213689&creative=392969&o=1&a=0470533331" width="1" height="1" /><a href="http://www.amazon.com/Beginning-SharePoint-2010-Development-Guides/dp/0470584637?ie=UTF8&tag=netgood-20&link_code=bil&camp=213689&creative=392969" target="_blank" imageanchor="1"><img alt="Beginning SharePoint 2010 Development (Wrox Beginning Guides)" src="http://ws.amazon.com/widgets/q?MarketPlace=US&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=_SL160_&ASIN=0470584637&tag=netgood-20" /></a><a href="http://www.amazon.com/Microsoft-SharePoint-2010-Solutions-Professionals/dp/1430228652?ie=UTF8&tag=netgood-20&link_code=bil&camp=213689&creative=392969" target="_blank" imageanchor="1"><img alt="Microsoft SharePoint 2010: Building Solutions for SharePoint 2010 (Books for Professionals by Professionals)" src="http://ws.amazon.com/widgets/q?MarketPlace=US&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=_SL160_&ASIN=1430228652&tag=netgood-20" /></a><img style="border-bottom-style: none !important; padding-bottom: 0px !important; margin: 0px; border-left-style: none !important; padding-left: 0px !important; padding-right: 0px !important; border-top-style: none !important; border-right-style: none !important; padding-top: 0px !important" border="0" alt="alt" src="http://www.assoc-amazon.com/e/ir?t=netgood-20&l=bil&camp=213689&creative=392969&o=1&a=1430228652" width="1" height="1" /><a href="http://www.amazon.com/Microsoft%C2%AE-SharePoint%C2%AE-2010-Administrators-Companion/dp/0735627207?ie=UTF8&tag=netgood-20&link_code=bil&camp=213689&creative=392969" target="_blank" imageanchor="1"><img alt="Microsoft® SharePoint® 2010 Administrator's Companion" src="http://ws.amazon.com/widgets/q?MarketPlace=US&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=_SL160_&ASIN=0735627207&tag=netgood-20" /></a><img style="border-bottom-style: none !important; padding-bottom: 0px !important; margin: 0px; border-left-style: none !important; padding-left: 0px !important; padding-right: 0px !important; border-top-style: none !important; border-right-style: none !important; padding-top: 0px !important" border="0" alt="alt" src="http://www.assoc-amazon.com/e/ir?t=netgood-20&l=bil&camp=213689&creative=392969&o=1&a=0735627207" width="1" height="1" /> <br /> <br />In SharePoint 2010, you can add "Word Automation Services" from Central Admin as shown below <p><a href="http://lh3.ggpht.com/_bxrn2DJZgfo/Tb5hcxNJHPI/AAAAAAAAAew/n9XO4XBcL-Q/s1600-h/image%5B3%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_bxrn2DJZgfo/Tb5heRfs56I/AAAAAAAAAe0/5DHl_MeKltI/image_thumb%5B1%5D.png?imgmax=800" width="469" height="484" /></a> </p> <p>Once the service is in place, we can easily convert Word documents to PDF as shown in below snippet</p> <p><span style="color: blue">private void</span>ConvertDocFileToPDF(SPFile filex) <br />{            <br />    ConversionJobSettings settings = <span style="color: blue">new</span>ConversionJobSettings(); <br />    settings.OutputFormat = SaveFormat.PDF; <br />    ConversionJob job = <span style="color: blue">new</span>ConversionJob(ConfigurationHelper.Instance.DocumentConversionService, settings);    <br />    job.UserToken = SPUserToken.SystemAccount; <br />    <span style="color: blue">string</span>pdfFile = filex.Url.Replace(<span style="color: #a31515">"docx"</span>, <span style="color: #a31515">"pdf"</span>); <br />    job.AddFile(<span style="color: #a31515">"http://<your site URL>/"</span>+ filex.Url, <span style="color: #a31515">"http://<your site URL>/"</span>+ pdfFile); <br />    job.Start(); <br />} <br /></p> <p>Ensure to add reference to “Microsoft.Office.Word.Server” present under 14 hive “ISAPI” folder.</p> <p>To test this, run “Word Automation Services Timer Job”</p> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com1tag:blogger.com,1999:blog-9164837514531450619.post-35933887234933019152011-05-02T00:29:00.000-07:002011-05-02T00:29:36.628-07:00DataTable to GenericList | C# and LINQ<pre class="code"><span style="color: blue;"><a href="http://www.amazon.com/Programming-Microsoft%C2%AE-LINQ-Microsoft-Framework/dp/0735640572?ie=UTF8&tag=netgood-20&link_code=bil&camp=213689&creative=392969" imageanchor="1" target="_blank"><img alt="Programming Microsoft® LINQ in Microsoft .NET Framework 4" src="http://ws.amazon.com/widgets/q?MarketPlace=US&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=_SL160_&ASIN=0735640572&tag=netgood-20" /></a><img alt="" border="0" height="1" src="http://www.assoc-amazon.com/e/ir?t=netgood-20&l=bil&camp=213689&creative=392969&o=1&a=0735640572" style="border: none !important; margin: 0px !important; padding: 0px !important;" width="1" /> </span></pre><pre class="code"><span style="color: blue;">using </span>System;
<span style="color: blue;">using </span>System.Data;
<span style="color: blue;">namespace </span>ConsoleApplication1
{
<span style="color: blue;">class </span><span style="color: #2b91af;">Program
</span>{
<span style="color: blue;">static void </span>Main(<span style="color: blue;">string</span>[] args)
{
<span style="color: #2b91af;">DataTable </span>table = <span style="color: blue;">new </span><span style="color: #2b91af;">DataTable
</span>{
Columns = {
{<span style="color: #a31515;">"Id"</span>, <span style="color: blue;">typeof</span>(<span style="color: blue;">int</span>)},
{<span style="color: #a31515;">"Name"</span>, <span style="color: blue;">typeof</span>(<span style="color: blue;">string</span>)}
}
};
table.Rows.Add(1, <span style="color: #a31515;">"Amal"</span>);
table.Rows.Add(1, <span style="color: #a31515;">"Fousiya"</span>);
table.Rows.Add(1, <span style="color: #a31515;">"Munna"</span>);
table.Rows.Add(1, <span style="color: #a31515;">"Hussain"</span>);
<span style="color: blue;">var </span>listOfEmployees = <span style="color: blue;">from </span>row <span style="color: blue;">in </span>table.AsEnumerable()
<span style="color: blue;">select new </span><span style="color: #2b91af;">Employee
</span>{
Id = row.Field<<span style="color: blue;">int</span>>(<span style="color: #a31515;">"Id"</span>),
Name = row.Field<<span style="color: #2b91af;">String</span>>(<span style="color: #a31515;">"Name"</span>)
};
<span style="color: blue;">foreach </span>(<span style="color: #2b91af;">Employee </span>emp <span style="color: blue;">in </span>listOfEmployees)
{
<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"{0} {1}"</span>, emp.Id, emp.Name);
}
}
}
<span style="color: blue;">class </span><span style="color: #2b91af;">Employee
</span>{
<span style="color: blue;">public int </span>Id { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
<span style="color: blue;">public </span><span style="color: #2b91af;">String </span>Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
}
}</pre><br />
<a href="http://11011.net/software/vspaste"></a>A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-70310089133221127642011-04-01T08:03:00.000-07:002011-04-01T08:03:20.993-07:00MVP 2011 - For the second timeHi all,<br />
<br />
Today I got awarded as MVP for the second time in a row. See the mail i got...feeling really happy.<br />
<br />
From: <a href="mailto:support@mvpaward.com" target="_blank">support@mvpaward.com</a><br />
To: <a href="mailto:amal.hashim@hotmail.com" target="_blank">amal.hashim@hotmail.com</a><br />
CC: <a href="mailto:abhishek@microsoft.com" target="_blank">abhishek@microsoft.com</a><br />
Date: Fri, 1 Apr 2011 09:12:39 -0500<br />
Subject: Congratulations 2011 Microsoft MVP!<br />
<br />
<img /> <br />
<br />
Dear Amal Hashim,<br />
<br />
Congratulations! We are pleased to present you with the 2011 Microsoft® MVP Award! This award is given to exceptional technical community leaders who actively share their high quality, real world expertise with others. We appreciate your outstanding contributions in Visual C# technical communities during the past year.<br />
<br />
Also in this email:<br />
<br />
<ul><li>About your MVP Award Gift</li>
<li>How to access <a href="http://www.mvpaward.com/" target="_blank">www.mvpaward.com</a> to begin taking advantage of your award benefits</li>
<li>Your MVP Identification Number</li>
<li>MVP Award Program Code of Conduct</li>
</ul>The Microsoft MVP Award provides us the unique opportunity to celebrate and honor your significant contributions and say "Thank you for your technical leadership."<br />
<br />
Toby Richards<br />
General Manager <br />
Community & Online SupportA.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-33389891380462636572011-01-24T22:54:00.001-08:002011-01-24T22:54:43.266-08:00Color Coding SharePoint List – With or Without Grouping<p>Using jQuery we can easily color code lists in SharePoint 2007.</p> <p>Try using the following script</p> <pre class="csharpcode"><script type=<span class="str">"text/javascript"</span> src=<span class="str">"/_layouts/jquery-1.4.4/jquery-1.4.4.min.js"</span>></script><br /><br /><style type=<span class="str">"text/css"</span>><br />.MyStyle<br />{<br /> font-size: 11px;<br /> background-color: red;<br />}<br /></style><br /><script type=<span class="str">"text/javascript"</span>><br /><br />$(document).ready(<span class="kwrd">function</span>(){<br /><br />$(<span class="str">".ms-vb2:contains('Completed')"</span>).each(<span class="kwrd">function</span>(){<br />$(<span class="kwrd">this</span>).addClass(MyStyle);<br />});<br />});<br /><br /></script></pre><br /><br /><p><style type="text/css"><br />.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }</style>Problem with this approach is, it won’t work in views with grouping. The problem is, on grouping, collapsed group data is not rendered on page load. To tackle this we need to tweak scripts in INIT.js, but directly modifying is not a good practice. To overcome this we can override the method which is rendering the data as shown in the below script block.</p><br /><br /><pre class="csharpcode"><style type=<span class="str">"text/css"</span>><br />.MyStyle<br />{<br /> font-size: 11px;<br /> background-color: red;<br />}<br /></style><br /><br /><script type=<span class="str">"text/javascript"</span> src=<span class="str">"/_layouts/jquery-1.4.4/jquery-1.4.4.min.js"</span>></script><br /><br /><script type=<span class="str">"text/javascript"</span>><br /><br /><span class="rem">//wait until all of the SharePoint stuff is loaded...</span><br />_spBodyOnLoadFunctionNames.push(<span class="str">"CustomExpGroupByFix"</span>); <br /><br /><span class="kwrd">function</span> CustomExpGroupByFix() <br />{<br /> <span class="rem">//Replace the function in INIT.JS with our custom function</span><br /> window.ExpGroupReceiveData=<span class="kwrd">function</span>(x,y){ExpGroupReceiveDataC(x,y)}<br /><br />}<br /><br /><span class="rem">//This is the exact replica of INIT.JS "ExpGroupReceiveData", we have added color coding code at the end</span><br /><span class="kwrd">function</span> ExpGroupReceiveDataC(htmlToRender, groupName)<br />{<br /> <span class="kwrd">var</span> ctxId=<span class="str">"ctx"</span>+groupName.substring(0, groupName.indexOf(<span class="str">"-"</span>));<br /> <span class="kwrd">var</span> indexBeginCTXName=htmlToRender.indexOf(<span class="str">"CTXName=\""</span>);<br /> <span class="kwrd">if</span> (indexBeginCTXName !=-1)<br /> {<br /> <span class="kwrd">if</span> (ctxId !=<span class="str">"ctx1"</span>)<br /> {<br /> htmlToRender=htmlToRender.replace(/ CTXName=\<span class="str">"ctx1\" /g, "</span> CTXName=\<span class="str">""</span>+ctxId+<span class="str">"\" "</span>);<br /> }<br /> }<br /> <span class="kwrd">var</span> needOuterWrap=<span class="kwrd">false</span>;<br /> <span class="kwrd">if</span> (htmlToRender.length < 4)<br /> {<br /> needOuterWrap=<span class="kwrd">true</span>;<br /> }<br /> <span class="kwrd">else</span> <span class="kwrd">if</span> (htmlToRender.substring(0,3) !=<span class="str">"<tr"</span>)<br /> {<br /> needOuterWrap=<span class="kwrd">true</span>;<br /> }<br /> <span class="kwrd">if</span> (needOuterWrap)<br /> {<br /> htmlToRender=<span class="str">"<TR><TD>"</span>+htmlToRender+<span class="str">"</TD></TR>"</span>;<br /> }<br /> ExpGroupRenderData(htmlToRender, groupName, <span class="str">"true"</span>);<br /> g_ExpGroupInProgress=<span class="kwrd">false</span>;<br /> <span class="kwrd">if</span> (g_ExpGroupQueue.length > 0)<br /> {<br /> ExpGroupFetchData(g_ExpGroupQueue.shift());<br /> }<br /> <br /> <span class="rem">//Color coding the cells</span><br /> $(<span class="str">".ms-vb2:contains('Completed')"</span>).each(<span class="kwrd">function</span>(){<br /> $(<span class="kwrd">this</span>).addClass(MyStyle);<br /> });<br />}<br /></script></pre><br /><br /><p>To use this script, edit the page –> add a content editor webpart.</p> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com2tag:blogger.com,1999:blog-9164837514531450619.post-37040601587668748572010-10-22T05:32:00.001-07:002010-10-22T23:48:41.256-07:00MOSS | Using WSPBuilder for Creating a List Instance<iframe style="width: 131px; padding-right: 10px; height: 245px; padding-top: 5px" marginheight="0" src="http://rcm.amazon.com/e/cm?t=netgood-20&o=1&p=8&l=bpl&asins=1430228652&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" frameborder="0" marginwidth="0" scrolling="no" align="left"></iframe>In this post I am going to explain how we can make use of WSPBuilder for creating a List instance. <br />First we need to install SharePoint Solution Generator. You can download the same from here <br /><a title="http://download.microsoft.com/download/4/0/b/40b62080-6295-4d63-b396-d779fb9b4449/VSeWSSv12.exe" href="http://download.microsoft.com/download/4/0/b/40b62080-6295-4d63-b396-d779fb9b4449/VSeWSSv12.exe">http://download.microsoft.com/download/4/0/b/40b62080-6295-4d63-b396-d779fb9b4449/VSeWSSv12.exe</a> <br />Now follow the steps for generating List Definition. Here I am going to create a List Definition of a List named “Configurations” <br />Start SharePoint solution generator <br /><a href="http://lh6.ggpht.com/_bxrn2DJZgfo/TMGB7eLzvHI/AAAAAAAAAbI/GvIpeSn22Xk/s1600-h/image%5B5%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_bxrn2DJZgfo/TMGB9ozES4I/AAAAAAAAAbM/inhQXcMGMao/image_thumb%5B3%5D.png?imgmax=800" width="644" height="472" /></a> <br />Select List Definition and click on Next button <br /><a href="http://lh6.ggpht.com/_bxrn2DJZgfo/TMGCEUSTmmI/AAAAAAAAAbQ/SJVvStF0iaU/s1600-h/image%5B9%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_bxrn2DJZgfo/TMGCXZFxtWI/AAAAAAAAAbU/3BBw865hG18/image_thumb%5B5%5D.png?imgmax=800" width="644" height="472" /></a> <br />Specify the Site Url where the list is present and click Next <br /><a href="http://lh3.ggpht.com/_bxrn2DJZgfo/TMGCfn0rYNI/AAAAAAAAAbY/06q1a0rSpfQ/s1600-h/image%5B13%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_bxrn2DJZgfo/TMGC6V-n10I/AAAAAAAAAbc/KMUNGwTRK2g/image_thumb%5B7%5D.png?imgmax=800" width="644" height="475" /></a> <br />Select the List and click Next <br /><a href="http://lh4.ggpht.com/_bxrn2DJZgfo/TMGDEMUu2NI/AAAAAAAAAbg/qc6jKB-fndM/s1600-h/image%5B17%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_bxrn2DJZgfo/TMGDKhan76I/AAAAAAAAAbk/WsqRnnnI2IA/image_thumb%5B9%5D.png?imgmax=800" width="644" height="473" /></a> <br />Provide a project name and path, then click Next <br /><a href="http://lh6.ggpht.com/_bxrn2DJZgfo/TMGDOU-v9TI/AAAAAAAAAbo/1SzL0wHkJRQ/s1600-h/image%5B21%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_bxrn2DJZgfo/TMGDUd9nopI/AAAAAAAAAbs/TRNbvvDWmhk/image_thumb%5B11%5D.png?imgmax=800" width="644" height="469" /></a> <br />Click Finish <br /> <a href="http://lh4.ggpht.com/_bxrn2DJZgfo/TMKFQ_FetfI/AAAAAAAAAco/AHeaC5aHTVU/s1600-h/image%5B4%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_bxrn2DJZgfo/TMKFSKUplnI/AAAAAAAAAcs/sd6pnKgwy6M/image_thumb%5B1%5D.png?imgmax=800" width="644" height="473" /></a> <br />Before clicking Exit, click “Click here to open the generated solution” and click Exit <br /><a href="http://lh5.ggpht.com/_bxrn2DJZgfo/TMGDbLknJII/AAAAAAAAAb4/gxzN-9krYlk/s1600-h/image%5B29%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_bxrn2DJZgfo/TMGDh-EAXuI/AAAAAAAAAb8/EIUatiH4mtg/image_thumb%5B15%5D.png?imgmax=800" width="644" height="356" /></a> <br />You can see the “Configurations” folder. Let it be opened. Now open visual studio and create a WSPBuilder project <br /><a href="http://lh5.ggpht.com/_bxrn2DJZgfo/TMGDk7_YswI/AAAAAAAAAcA/nuKi_62-IFM/s1600-h/image%5B33%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_bxrn2DJZgfo/TMGDoGlL43I/AAAAAAAAAcE/mrFmPiQuCW8/image_thumb%5B17%5D.png?imgmax=800" width="644" height="392" /></a> <br />Once the project is created, add a new blank feature <br /><a href="http://lh4.ggpht.com/_bxrn2DJZgfo/TMGDrHlum3I/AAAAAAAAAcI/j6Q4GgQxCik/s1600-h/image%5B37%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_bxrn2DJZgfo/TMGDuH-1A6I/AAAAAAAAAcM/mdHY8exuaMA/image_thumb%5B19%5D.png?imgmax=800" width="644" height="386" /></a> <br />Now, copy the folder “Configurations” and paste it under the feature folder as shown below <br /><a href="http://lh5.ggpht.com/_bxrn2DJZgfo/TMGDvimxicI/AAAAAAAAAcQ/Fv_U9Prsnw4/s1600-h/image%5B44%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_bxrn2DJZgfo/TMGDynl5YEI/AAAAAAAAAcU/6E0ehJyinBA/image_thumb%5B24%5D.png?imgmax=800" width="351" height="484" /></a> <br />Open the ListDefinition.xml file and copy the following content <br /><a href="http://lh5.ggpht.com/_bxrn2DJZgfo/TMGESbOmA7I/AAAAAAAAAcY/FgeiiuQBsaQ/s1600-h/image%5B48%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_bxrn2DJZgfo/TMGETm3x3zI/AAAAAAAAAcc/A4KaC3pmJdE/image_thumb%5B26%5D.png?imgmax=800" width="644" height="434" /></a> <br />Once the content is copied, paste it in the elements.xml file as shown below and delete the file "ListDefinition.xml" from visual studion project <br /><a href="http://lh6.ggpht.com/_bxrn2DJZgfo/TMGEUtd-zYI/AAAAAAAAAcg/ofqXres3AN8/s1600-h/image%5B54%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_bxrn2DJZgfo/TMGEVvvwDOI/AAAAAAAAAck/CfBzfY6lAj8/image_thumb%5B30%5D.png?imgmax=800" width="595" height="484" /></a> <br />You can also see ListInstance, make a similar entry in your elements.xml file. Also replace the FeatureId with the FeatureId specified in the feature.xml file <br />Now build the wsp, deploy it and activate the feature. This will add the list to the site. <br />Hope this was helpful!!! A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com4tag:blogger.com,1999:blog-9164837514531450619.post-6905236615582879492010-10-21T02:41:00.001-07:002010-10-21T02:54:50.221-07:00MOSS | Using People Editor Control<iframe align="left" frameborder="0" marginheight="0" marginwidth="0" scrolling="no" src="http://rcm.amazon.com/e/cm?t=netgood-20&o=1&p=8&l=bpl&asins=0470529423&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;"></iframe><br />
People editor can be used whenever we want the user to select user, AD groups or SharePoint groups. For using the control, 1st we need to register the assembly as shown below<br />
<pre class="code"><span style="background: none repeat scroll 0% 0% rgb(255, 238, 98);"><%</span><span style="color: blue;">@ </span><span style="color: #a31515;">register </span><span style="color: red;">tagprefix</span><span style="color: blue;">="SharePointWebControls" </span><span style="color: red;">namespace</span><span style="color: blue;">="Microsoft.SharePoint.WebControls"
</span><span style="color: red;">assembly</span><span style="color: blue;">="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" </span><span style="background: none repeat scroll 0% 0% rgb(255, 238, 98);">%></span></pre>Once this is done, we can use the following tag for placing the control <br />
<pre class="code"><span style="color: blue;"><</span><span style="color: #a31515;">SharePointWebControls</span><span style="color: blue;">:</span><span style="color: #a31515;">PeopleEditor </span><span style="color: red;">ID</span><span style="color: blue;">="ppeUser" </span><span style="color: red;">runat</span><span style="color: blue;">="server" </span><span style="color: red;">Rows</span><span style="color: blue;">="1"
</span><span style="color: red;">CheckButtonImageName </span><span style="color: blue;">= "/_layouts/Images/user.png"
</span><span style="color: red;">BrowseButtonImageName </span><span style="color: blue;">= "/_layouts/Images/addressbook.png"
</span><span style="color: red;">PlaceButtonsUnderEntityEditor</span><span style="color: blue;">="false" </span><span style="color: red;">MultiSelect</span><span style="color: blue;">="false" </span><span style="color: red;">AutoPostBack</span><span style="color: blue;">="true"/></span></pre><br />
In C#, the following code demonstrate the usage<br />
<br />
this.ppeUser.CommaSeparatedAccounts;<br />
<br />
Using the above code we can get information user has selected/entered in the people editor control.<br />
<br />
<span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> UpdatePeoplePicker(<span class="kwrd">string</span> login, PeopleEditor editor)<br />
{<br />
ArrayList list = <span class="kwrd">new</span> ArrayList();<br />
PickerEntity entity = <span class="kwrd">new</span> PickerEntity();<br />
entity.Key = login;<br />
entity = editor.ValidateEntity(entity);<br />
list.Add(entity);<br />
editor.UpdateEntities(list);<br />
}<br />
<br />
The above code can be used to update the People Editor control using code.<br />
<br />
<br />
We can restrict the selection of the People Editor using the SelectionSet property. It accepts the following values<br />
<br />
User – In case the selection to be restricted only for users<br />
AD – In case the selection to be restricted only for AD groups<br />
SPGroup – In case the selection to be restricted only for SharePoint Groups<br />
<br />
Hope this was helpful!!!A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-17822632962178783762010-10-08T00:29:00.001-07:002010-10-08T00:29:48.158-07:00MOSS 2007 | Enumerate User Profile Properties<p>Below is the code to enumerate the User Profile Properties</p> <pre class="csharpcode"><span class="kwrd">using</span> System;<br /><span class="kwrd">using</span> Microsoft.Office.Server;<br /><span class="kwrd">using</span> Microsoft.Office.Server.UserProfiles;<br /><span class="kwrd">using</span> Microsoft.SharePoint;<br /><br /><span class="kwrd">namespace</span> ConsoleApplication1<br />{<br /> <span class="kwrd">class</span> Program<br /> {<br /> <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)<br /> {<br /> <span class="kwrd">using</span> (SPSite site = <span class="kwrd">new</span> SPSite(<span class="str">"http://server:port/"</span>))<br /> {<br /> ServerContext context = <br /> ServerContext.GetContext(site);<br /> UserProfileManager profileManager = <span class="kwrd">new</span> UserProfileManager(context);<br /> <span class="kwrd">string</span> sAccount = <span class="str">"domain\\user"</span>;<br /> UserProfile u = profileManager.GetUserProfile(sAccount);<br /> PropertyCollection props = profileManager.Properties;<br /><br /> <span class="kwrd">foreach</span> (Property prop <span class="kwrd">in</span> props)<br /> {<br /> Console.WriteLine(prop.DisplayName + <span class="str">">>"</span> + prop.Name );<br /> }<br /> }<br /> }<br /> }<br />}</pre><br /><style type="text/css"><br />.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }</style> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-80752849552334068612010-09-24T04:40:00.001-07:002010-09-24T04:40:23.982-07:00C# | Automating Facebook Login using WebBrowser Control<p>Create a new windows forms application project.</p> <p>Add two button and place the web browser control as shown below. Rename the button as “btnShowPage” and “btnLogin”.</p> <p><a href="http://lh3.ggpht.com/_bxrn2DJZgfo/TJyNogLkEyI/AAAAAAAAAbA/s6GJAOEAKs0/s1600-h/image%5B3%5D.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="484" alt="image" src="http://lh5.ggpht.com/_bxrn2DJZgfo/TJyOIyElcGI/AAAAAAAAAbE/SoA_PH7QEYE/image_thumb%5B1%5D.png?imgmax=800" width="543" border="0" /></a> </p> <p>On form load event use the following code</p> <pre class="code"><span style="color: blue">private void </span>Form1_Load(<span style="color: blue">object </span>sender, <span style="color: #2b91af">EventArgs </span>e)<br />{<br /> btnLogin.Enabled = <span style="color: blue">false</span>;<br />}</pre><br /><a href="http://11011.net/software/vspaste"></a><br /><br /><p>Now on button btnShowPage use the following code</p><br /><br /><pre class="code"><span style="color: blue">private void </span>btnShowPage_Click(<span style="color: blue">object </span>sender, <span style="color: #2b91af">EventArgs </span>e)<br />{<br /> webBrowser1.Navigate(<span style="color: #a31515">"https://login.facebook.com/login.php?login_attempt=1"</span>);<br /> webBrowser1.DocumentCompleted += <span style="color: blue">new </span><span style="color: #2b91af">WebBrowserDocumentCompletedEventHandler</span>(webBrowser1_DocumentCompleted);<br />}</pre><br /><br /><pre class="code"><span style="color: blue">void </span>webBrowser1_DocumentCompleted(<span style="color: blue">object </span>sender, <span style="color: #2b91af">WebBrowserDocumentCompletedEventArgs </span>e)<br />{<br /> <span style="color: blue">string </span>s = webBrowser1.DocumentText;<br /> btnLogin.Enabled = <span style="color: blue">true</span>;<br />}</pre><br /><a href="http://11011.net/software/vspaste"></a><br /><br /><p><a href="http://11011.net/software/vspaste"></a>On btnLogin click event use the following code</p><br /><br /><pre class="code"><span style="color: blue">private void </span>btnLogin_Click(<span style="color: blue">object </span>sender, <span style="color: #2b91af">EventArgs </span>e)<br />{<br /> <span style="color: #2b91af">HtmlElement </span>ele = webBrowser1.Document.GetElementById(<span style="color: #a31515">"email"</span>);<br /> <span style="color: blue">if </span>(ele != <span style="color: blue">null</span>)<br /> ele.InnerText = <span style="color: #a31515">"amalhashim@gmail.com"</span>;<br /><br /> ele = webBrowser1.Document.GetElementById(<span style="color: #a31515">"pass"</span>);<br /> <span style="color: blue">if </span>(ele != <span style="color: blue">null</span>)<br /> ele.InnerText = <span style="color: #a31515">"password"</span>;<br /><br /> ele = webBrowser1.Document.GetElementById(<span style="color: #a31515">"Login"</span>);<br /> <span style="color: blue">if </span>(ele != <span style="color: blue">null</span>)<br /> ele.InvokeMember(<span style="color: #a31515">"click"</span>);<br />}</pre><br /><a href="http://11011.net/software/vspaste"></a><br /><br /><p>That’s it :-)</p> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com5tag:blogger.com,1999:blog-9164837514531450619.post-74547907706673804962010-08-30T04:12:00.001-07:002010-08-30T04:12:32.849-07:00Custom Application MasterPage | Sharepoint 2007<p align="justify">Recently I have involved in branding one of our clients sharepoint portal. As part of the branding process we needed to modify the application.master file, so even the layout pages  will have the same look and feel. As part of the investigation, we come up with the best approach for doing this.</p> <p align="justify">1. Create a custom master page, with all the placeholders which are available in the original application.master file. Its better to take a copy of the application.master file and add the styles/images etc as you wish</p> <p align="justify">2. Create a custom HttpModule which will set the new custom master page if the current page is having the master page “application.master”. We preferred HttpModule on top of HttpHandler because HttpModule can be deployed only to the web applications we wants, while HttpHandler will hit across all web applications.</p> <p align="justify">Below is the code i have created for the http module.</p> <pre class="code"><span style="color: blue">using </span>System;<br /><span style="color: blue">using </span>System.Web;<br /><span style="color: blue">using </span>System.Web.UI;<br /><span style="color: blue">using </span>Microsoft.SharePoint;<br /><br /><span style="color: blue">namespace </span>Company.HttpModules<br />{<br /> <span style="color: gray">/// <summary><br /> /// </span><span style="color: green">All the layout pages will be using application.master<br /> </span><span style="color: gray">/// </span><span style="color: green">inorder to make the application consistent. we have created a custom application master page<br /> </span><span style="color: gray">/// </span><span style="color: green">this module will check whether the current request is having the master page as application.master<br /> </span><span style="color: gray">/// </span><span style="color: green">then change it to our custom page<br /> </span><span style="color: gray">/// </summary><br /> </span><span style="color: blue">public class </span><span style="color: #2b91af">ApplicationMasterModule </span>: <span style="color: #2b91af">IHttpModule<br /> </span>{<br /> <span style="color: blue">public void </span>Init(<span style="color: #2b91af">HttpApplication </span>context)<br /> {<br /> <span style="color: blue">if </span>(context == <span style="color: blue">null</span>)<br /> <span style="color: blue">throw new </span><span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"context"</span>);<br /><br /> context.PreRequestHandlerExecute += <span style="color: blue">new </span><span style="color: #2b91af">EventHandler</span>(context_PreRequestHandlerExecute);<br /> }<br /><br /> <span style="color: blue">void </span>context_PreRequestHandlerExecute(<span style="color: blue">object </span>sender, <span style="color: #2b91af">EventArgs </span>e)<br /> {<br /> <span style="color: #2b91af">Page </span>page = <span style="color: #2b91af">HttpContext</span>.Current.CurrentHandler <span style="color: blue">as </span><span style="color: #2b91af">Page</span>;<br /> <span style="color: blue">if </span>(page != <span style="color: blue">null</span>)<br /> {<br /> page.PreInit += <span style="color: blue">new </span><span style="color: #2b91af">EventHandler</span>(page_PreInit);<br /> }<br /> }<br /><br /> <span style="color: blue">void </span>page_PreInit(<span style="color: blue">object </span>sender, <span style="color: #2b91af">EventArgs </span>e)<br /> {<br /> <span style="color: #2b91af">Page </span>page = sender <span style="color: blue">as </span><span style="color: #2b91af">Page</span>;<br /> <span style="color: blue">if </span>(page != <span style="color: blue">null</span>)<br /> {<br /> <span style="color: blue">if </span>(page.MasterPageFile != <span style="color: blue">null</span>)<br /> {<br /> <span style="color: blue">if </span>(page.MasterPageFile.Contains(<span style="color: #a31515">"application.master"</span>))<br /> {<br /> <br /><br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SPSite </span>connectSite = <span style="color: blue">new </span><span style="color: #2b91af">SPSite</span>(<span style="color: #2b91af">SPContext</span>.Current.Site.Url))<br /> {<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SPWeb </span>currentWeb = connectSite.RootWeb)<br /> {<br /> <span style="color: blue">string </span>url = currentWeb.ServerRelativeUrl + <span style="color: #a31515">"/_catalogs/masterpage/our_custom_app.master"</span>;<br /> page.MasterPageFile = url;<br /> }<br /> }<br /> }<br /> }<br /> }<br /> }<br /><br /> <span style="color: blue">public void </span>Dispose()<br /> {<br /> }<br /> }<br />}</pre><br /><a href="http://11011.net/software/vspaste"></a><br /><br /><p align="justify">Now comes the important aspect of deployment. Its better to create the master page as well as http module to be deployed as a feature. See the feature.xml </p><br /><br /><pre class="code"><span style="color: blue"><?</span><span style="color: #a31515">xml </span><span style="color: red">version</span><span style="color: blue">=</span>"<span style="color: blue">1.0</span>" <span style="color: red">encoding</span><span style="color: blue">=</span>"<span style="color: blue">utf-8</span>"<span style="color: blue">?><br /><</span><span style="color: #a31515">Feature </span><span style="color: red">Id</span><span style="color: blue">=</span>"<span style="color: blue">b1531032-19b5-4fb1-87a6-6892b280c90d</span>"<br /> <span style="color: red">Title</span><span style="color: blue">=</span>"<span style="color: blue">Custom Application Master Page</span>"<br /> <span style="color: red">Description</span><span style="color: blue">=</span>"<span style="color: blue">Custom Application Master Page</span>"<br /> <span style="color: red">Version</span><span style="color: blue">=</span>"<span style="color: blue">12.0.0.0</span>"<br /> <span style="color: red">Hidden</span><span style="color: blue">=</span>"<span style="color: blue">FALSE</span>"<br /> <span style="color: red">Scope</span><span style="color: blue">=</span>"<span style="color: blue">Site</span>"<br /> <span style="color: red">DefaultResourceFile</span><span style="color: blue">=</span>"<span style="color: blue">core</span>"<br /> <span style="color: red">ReceiverAssembly</span><span style="color: blue">=</span>"<span style="color: blue">Company.MasterPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6c27a5016ad8e167</span>"<br /> <span style="color: red">ReceiverClass</span><span style="color: blue">=</span>"<span style="color: blue">Company.MasterPages.CustomAppMasterPage</span>" <br /> <span style="color: red">xmlns</span><span style="color: blue">=</span>"<span style="color: blue">http://schemas.microsoft.com/sharepoint/</span>"<span style="color: blue">><br /> <</span><span style="color: #a31515">ElementManifests</span><span style="color: blue">><br /> <</span><span style="color: #a31515">ElementManifest </span><span style="color: red">Location</span><span style="color: blue">=</span>"<span style="color: blue">elements.xml</span>"<span style="color: blue">/><br /> <</span><span style="color: #a31515">ElementFile </span><span style="color: red">Location</span><span style="color: blue">=</span>"<span style="color: blue">our_custom_app.master</span>" <span style="color: blue">/><br /> </</span><span style="color: #a31515">ElementManifests</span><span style="color: blue">><br /></</span><span style="color: #a31515">Feature</span><span style="color: blue">><br /></span></pre><br /><a href="http://11011.net/software/vspaste"></a><br /><br /><p align="justify">and elements.xml file</p><br /><br /><pre class="code"><span style="color: blue"><?</span><span style="color: #a31515">xml </span><span style="color: red">version</span><span style="color: blue">=</span>"<span style="color: blue">1.0</span>" <span style="color: red">encoding</span><span style="color: blue">=</span>"<span style="color: blue">utf-8</span>" <span style="color: blue">?><br /><</span><span style="color: #a31515">Elements </span><span style="color: red">xmlns</span><span style="color: blue">=</span>"<span style="color: blue">http://schemas.microsoft.com/sharepoint/</span>"<span style="color: blue">><br /> <</span><span style="color: #a31515">Module </span><span style="color: red">Name</span><span style="color: blue">=</span>"<span style="color: blue">CustomApplicationMasterPage</span>"<br /> <span style="color: red">Url</span><span style="color: blue">=</span>"<span style="color: blue">_catalogs/masterpage</span>"<br /> <span style="color: red">Path</span><span style="color: blue">=</span>""<br /> <span style="color: red">RootWebOnly</span><span style="color: blue">=</span>"<span style="color: blue">False</span>"<span style="color: blue">><br /> <</span><span style="color: #a31515">File </span><span style="color: red">Url</span><span style="color: blue">=</span>"<span style="color: blue">our_custom_app.master</span>"<br /> <span style="color: red">Type</span><span style="color: blue">=</span>"<span style="color: blue">GhostableInLibrary</span>" <span style="color: red">IgnoreIfAlreadyExists</span><span style="color: blue">=</span>"<span style="color: blue">FALSE</span>"<span style="color: blue">><br /> <</span><span style="color: #a31515">Property </span><span style="color: red">Name</span><span style="color: blue">=</span>"<span style="color: blue">ContentType</span>"<br /> <span style="color: red">Value</span><span style="color: blue">=</span>"<span style="color: blue">$Resources:cmscore,contenttype_-9masterpage_name;</span>" <span style="color: blue">/><br /> <</span><span style="color: #a31515">Property </span><span style="color: red">Name</span><span style="color: blue">=</span>"<span style="color: blue">PublishingPreviewImage</span>"<br /> <span style="color: red">Value</span><span style="color: blue">=</span>"" <span style="color: blue">/><br /> <</span><span style="color: #a31515">Property </span><span style="color: red">Name</span><span style="color: blue">=</span>"<span style="color: blue">Description</span>"<br /> <span style="color: red">Value</span><span style="color: blue">=</span>"<span style="color: blue">Custom Application Master Page</span>"<span style="color: blue">/><br /> </</span><span style="color: #a31515">File</span><span style="color: blue">> <br /> </</span><span style="color: #a31515">Module</span><span style="color: blue">><br /></</span><span style="color: #a31515">Elements</span><span style="color: blue">></span></pre><br /><a href="http://11011.net/software/vspaste"></a><br /><br /><p align="justify">See the Feature Receiver assembly which will add the web.config entries so the HttpModule will work without manually modifying the web configuration file</p><br /><br /><pre class="code"><span style="color: blue">using </span>System;<br /><span style="color: blue">using </span>System.Collections.Generic;<br /><span style="color: blue">using </span>System.Text;<br /><span style="color: blue">using </span>Microsoft.SharePoint;<br /><span style="color: blue">using </span>Microsoft.SharePoint.Administration;<br /><span style="color: blue">using </span>System.Globalization;<br /><span style="color: blue">using </span>System.Diagnostics;<br /><br /><span style="color: blue">namespace </span>Company.UIUpdates<br />{ <br /> <span style="color: gray">/// <summary><br /> /// </span><span style="color: green">On Feature Activation => Add HttpModule Entry in Web.Config file<br /> </span><span style="color: gray">/// </span><span style="color: green">On Feature DeActivation => Remove the HttpModule Entry from Web.Config file<br /> </span><span style="color: gray">/// </summary><br /> </span><span style="color: blue">class </span><span style="color: #2b91af">CustomAppMasterPage </span>: <span style="color: #2b91af">SPFeatureReceiver<br /> </span>{<br /> <span style="color: blue">static </span><span style="color: #2b91af">SPWebConfigModification </span>CreateModification(<span style="color: blue">string </span>Name, <span style="color: blue">string </span>XPath, <span style="color: blue">string </span>Value)<br /> {<br /> <span style="color: #2b91af">SPWebConfigModification </span>modification = <span style="color: blue">new </span><span style="color: #2b91af">SPWebConfigModification</span>(Name, XPath);<br /> modification.Owner = <span style="color: #a31515">"Custom Application Master Page.wsp"</span>;<br /> modification.Sequence = 0;<br /> modification.Type = <span style="color: #2b91af">SPWebConfigModification</span>.<span style="color: #2b91af">SPWebConfigModificationType</span>.EnsureChildNode;<br /> modification.Value = Value;<br /> <span style="color: blue">return </span>modification; <br /> }<br /><br /> <span style="color: blue">public override void </span>FeatureActivated(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties)<br /> { <br /> <span style="color: blue">try<br /> </span>{ <br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SPSite </span>site = properties.Feature.Parent <span style="color: blue">as </span><span style="color: #2b91af">SPSite</span>)<br /> {<br /> <span style="color: #2b91af">SPWebApplication </span>webApp = site.WebApplication;<br /> <span style="color: blue">string </span>name = <span style="color: #a31515">"add[@name='ApplicationMasterModule']"</span>;<br /> <span style="color: blue">string </span>path = <span style="color: #a31515">"configuration/system.web/httpModules"</span>;<br /> <span style="color: blue">string </span>value = <span style="color: #a31515">@"<add name=""ApplicationMasterModule"" type=""Company.HttpModules.ApplicationMasterModule,Company.HttpModules, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f2391931b556f037"" />"</span>;<br /> <span style="color: blue">if </span>(webApp != <span style="color: blue">null</span>)<br /> {<br /> <span style="color: #2b91af">SPWebConfigModification </span>modification = CreateModification(name, path, value);<br /> webApp.WebConfigModifications.Add(modification);<br /> webApp.Update();<br /> <span style="color: #2b91af">SPWebService</span>.ContentService.ApplyWebConfigModifications();<br /> <span style="color: #2b91af">SPWebService</span>.ContentService.WebApplications[webApp.Id].Update();<br /> <span style="color: green">//Applies the web config settings in all the web application in the farm<br /> </span><span style="color: #2b91af">SPWebService</span>.ContentService.WebApplications[webApp.Id].WebService.ApplyWebConfigModifications();<br /> }<br /> }<br /> }<br /> <span style="color: blue">catch </span>(<span style="color: #2b91af">Exception </span>ex)<br /> {<br /> WriteMessageToEventLog(ex.ToString());<br /> } <br /> }<br /><br /> <span style="color: blue">static private void </span>WriteMessageToEventLog(<span style="color: blue">string </span>message)<br /> {<br /> <span style="color: blue">string </span>EVENT_SOURCE = <span style="color: #a31515">"CustomAppMasterPage Feature Receiver"</span>;<br /> <span style="color: #2b91af">SPSecurity</span>.RunWithElevatedPrivileges(<span style="color: blue">delegate</span>()<br /> {<br /> <span style="color: blue">if </span>(!<span style="color: #2b91af">EventLog</span>.SourceExists(EVENT_SOURCE))<br /> {<br /> <span style="color: #2b91af">EventLog</span>.CreateEventSource(EVENT_SOURCE, <span style="color: #a31515">"Application"</span>);<br /> }<br /><br /> <span style="color: #2b91af">EventLog</span>.WriteEntry(EVENT_SOURCE, message);<br /> });<br /> }<br /><br /> <span style="color: blue">public override void </span>FeatureDeactivating(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties)<br /> {<br /> <span style="color: blue">try<br /> </span>{<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SPSite </span>site = properties.Feature.Parent <span style="color: blue">as </span><span style="color: #2b91af">SPSite</span>)<br /> {<br /> <span style="color: #2b91af">SPWebApplication </span>webApp = site.WebApplication;<br /> <span style="color: blue">string </span>name = <span style="color: #a31515">"add[@name='ApplicationMasterModule']"</span>;<br /> <span style="color: blue">string </span>path = <span style="color: #a31515">"configuration/system.web/httpModules"</span>;<br /> <span style="color: blue">string </span>value = <span style="color: #a31515">@"<add name=""ApplicationMasterModule"" type=""Company.HttpModules.ApplicationMasterModule,Company.HttpModules, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f2391931b556f037"" />"</span>;<br /> <span style="color: blue">if </span>(webApp != <span style="color: blue">null</span>)<br /> {<br /> <span style="color: #2b91af">SPWebConfigModification </span>modification = CreateModification(name, path, value);<br /> webApp.WebConfigModifications.Remove(modification);<br /> webApp.Update();<br /> <span style="color: #2b91af">SPWebService</span>.ContentService.ApplyWebConfigModifications();<br /> <span style="color: #2b91af">SPWebService</span>.ContentService.WebApplications[webApp.Id].Update();<br /> <span style="color: green">//Applies the web config settings in all the web application in the farm<br /> </span><span style="color: #2b91af">SPWebService</span>.ContentService.WebApplications[webApp.Id].WebService.ApplyWebConfigModifications();<br /> } <br /> }<br /> }<br /> <span style="color: blue">catch </span>(<span style="color: #2b91af">Exception </span>ex)<br /> {<br /> WriteMessageToEventLog(ex.ToString());<br /> } <br /> } <br /><br /> <span style="color: blue">public override void </span>FeatureInstalled(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties)<br /> {<br /> <br /> }<br /><br /> <span style="color: blue">public override void </span>FeatureUninstalling(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties)<br /> {<br /> <br /> }<br /> }<br />}</pre><br /><br /><p>As part of the WSP, we included the following</p><br /><br /><p>1. Custom Master Page</p><br /><br /><p>2. Http Module dll which will go into GAC</p><br /><br /><p>3. Feature Receiver</p><br /><br /><p>Hope this helps. Feel free to contact me if you have any queries.</p> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-87860380052084639322010-08-19T23:12:00.001-07:002010-08-19T23:12:42.791-07:00Microsoft Sync Framework 2.1 Software Development Kit (SDK) Released<p align="justify">Microsoft Sync Framework is a comprehensive synchronization platform that enables collaboration and offline scenarios for applications, services, and devices. Using Microsoft Sync Framework, developers can build applications that synchronize data from any source using any protocol over any network.</p> <p align="justify">Sync Framework 2.1 introduces new features that let you synchronize a SQL Server or SQL Server Compact database on your computer with a SQL Azure database. This release also introduces parameter-based filtering, the ability to remove synchronization scopes and templates from a database, and performance enhancements to make synchronization faster and easier. </p> <p><b>SQL Azure Synchronization </b></p> <strong></strong> <p align="justify"> <br />With Sync Framework 2.1, you can extend the reach of your data to the web by leveraging the Windows Azure Platform and SQL Azure Database. By synchronizing a SQL Server database on your business premises to SQL Azure, you make some or all of your data available on the web without the need to provide your customers with a connection to your on premises SQL Server database. After you configure your SQL Azure database for synchronization, users can take the data offline and store it in a client database, such as SQL Server Compact or SQL Server Express, so that your applications operate while disconnected and your customers can stay productive without the need for a reliable network connection. Changes made to data in the field can be synchronized back to the SQL Azure database and ultimately back to the on premises SQL Server database. Sync Framework 2.1 also includes features to interact well with the shared environment of Windows Azure and SQL Azure. These features include performance enhancements, the ability to define the maximum size of a transaction to avoid throttling, and automatic retries of a transaction if it is throttled by Windows Azure. Sync Framework gives you flexibility in the way you structure your synchronization community, but two typical ways are to use a 2-tier architecture or an N-tier architecture. </p> <ul> <li> <div align="justify">2-tier architecture: Sync Framework runs on the local computer and uses a SqlSyncProvider object to connect directly to the SQL Azure database without going through a middle tier or a web server, such as Internet Information Services (IIS). </div> </li> <li> <div align="justify">N-tier architecture: A Sync Framework database provider runs in a Windows Azure hosted service and communicates with a proxy provider that runs on the local computer.</div> </li> </ul> <p><b>Bulk Application of Changes </b></p> <strong></strong> <p align="justify"> <br />Sync Framework 2.1 takes advantage of the table-valued parameter feature of SQL Server 2008 and SQL Azure to apply multiple inserts, updates, and deletes by using a single stored procedure call, instead of requiring a stored procedure call to apply each change. This greatly increases performance of these operations and reduces the number of round trips between client and server during change application. Bulk procedures are created by default when a SQL Server 2008 or SQL Azure database is provisioned. </p> <p align="justify"> <br /><b>Parameter-based Filtering </b></p> <p align="justify"><strong></strong> <br />ync Framework 2.1 enables you to create parameter-based filters that control what data is synchronized. Parameter-based filters are particularly useful when users want to filter data based on a field that can have many different values, such as user ID or region, or a combination of two or more fields. Parameter-based filters are created in two steps. First, filter and scope templates are defined. Then, a filtered scope is created that has specific values for the filter parameters. This two-step process has the following advantages: </p> <ul> <li> <div align="justify">Easy to set up. A filter template is defined one time. Creating a filter template is the only action that requires permission to create stored procedures in the database server. This step is typically performed by a database administrator.</div> </li> <li> <div align="justify">Easy to subscribe. Clients specify parameter values to create and subscribe to filtered scopes on an as-needed basis. This step requires only permission to insert rows in synchronization tables in the database server. This step can be performed by a user.</div> </li> <li> <div align="justify">Easy to maintain. Even when several parameters are combined and lots of filtered scopes are created, maintenance is simple because a single, parameter-based procedure is used to enumerate changes.</div> </li> </ul> <p> <br /><b>Removing Scopes and Templates </b></p> <strong></strong> <p align="justify"> <br />Sync Framework 2.1 adds the SqlSyncScopeDeprovisioning and SqlCeSyncScopeDeprovisioning classes to enable you to easily remove synchronization elements from databases that have been provisioned for synchronization. By using these classes you can remove scopes, filter templates, and the associated metadata tables, triggers, and stored procedures from your databases. </p> <p align="justify"> <br /><b>Upgrading the Metadata Format </b></p> <strong></strong> <p align="justify"> <br />The metadata format for the database providers changed in Sync Framework 2.1. The new metadata format is incompatible with previous versions of the database providers. The upgrade to the new metadata format cannot be undone, and when you try to use an earlier version of the database providers to synchronize a database that is in the 2.1 format, Sync Framework throws an exception. However, the SqlSyncProvider class in Sync Framework 2.1 detects whether the metadata is in the 2.0 or 2.1 format, and operates in a backward compatibility mode to synchronize a database that contains metadata in the 2.0 format. Sync Framework can synchronize a database in the 2.0 format with a database in either the 2.0 or the 2.1 format. Therefore, it is not necessary to upgrade all of the databases in your synchronization community at the same time. For example, in an N-tier architecture you can upgrade the server Sync Framework components and database metadata format and continue to synchronize with clients that use Sync Framework 2.0. Clients can then upgrade when it is convenient for them to do so. </p> <p align="justify"> <br /><b>SQL Server Compact 3.5 SP2 Compatibility </b></p> <strong></strong> <p align="justify"> <br />The Sync Framework 2.1 SqlCeSyncProvider database provider object uses SQL Server Compact 3.5 SP2. Existing SQL Server Compact databases are automatically upgraded when Sync Framework connects to them. Among other new features, SQL Server Compact 3.5 SP2 makes available a change tracking API that provides the ability to configure, enable, and disable change tracking on a table, and to access the change tracking data for the table. SQL Server Compact 3.5 SP2 can be downloaded <a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=e497988a-c93a-404c-b161-3a0b323dce24">here</a>. </p> <p align="justify"> <br /><b>Sync Framework 2.1 Redistributable Package</b></p> <p align="justify"><strong></strong> <br />To download the Microsoft Sync Framework 2.1 redistributables, rather than the SDK package, click on the link: <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=c3b2f32b-12a9-40b8-9e83-c452ac546c49">Microsoft Sync Framework 2.1 Redistributable Package</a></p> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-52238405720990140942010-08-12T03:28:00.001-07:002010-08-12T04:51:23.231-07:00Sharepoint 2007 Deploying Styles as FeatureAssuming you have WSP Builder installed. <br />Open Visual Studio, From File –> New Project <br /><a href="http://lh3.ggpht.com/_bxrn2DJZgfo/TGPMGxZMR4I/AAAAAAAAAZ4/SXWpS-6bwEE/s1600-h/image%5B3%5D.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="415" alt="image" src="http://lh5.ggpht.com/_bxrn2DJZgfo/TGPMJwUCtNI/AAAAAAAAAZ8/5CXuR0nOSyc/image_thumb%5B1%5D.png?imgmax=800" width="687" border="0" /></a> <br />Clicking OK will create a solution with the following folder structure <br /><a href="http://lh6.ggpht.com/_bxrn2DJZgfo/TGPMM4PX2wI/AAAAAAAAAaA/C2xD6ACfCTw/s1600-h/image%5B7%5D.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="229" alt="image" src="http://lh3.ggpht.com/_bxrn2DJZgfo/TGPMQZUeQhI/AAAAAAAAAaE/HeKgGoj-FRc/image_thumb%5B3%5D.png?imgmax=800" width="370" border="0" /></a> <br />Now right click the Project “Test_Style” –> Add –> New Item <br /><a href="http://lh4.ggpht.com/_bxrn2DJZgfo/TGPMUOswNzI/AAAAAAAAAaI/RW290X0Dss0/s1600-h/image%5B11%5D.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="447" alt="image" src="http://lh4.ggpht.com/_bxrn2DJZgfo/TGPMXekHsTI/AAAAAAAAAaM/TEv5oOck12s/image_thumb%5B5%5D.png?imgmax=800" width="399" border="0" /></a> <br />From the Add New Item dialog Select WSPBuilder –> Blank Feature <br /><a href="http://lh6.ggpht.com/_bxrn2DJZgfo/TGPMZDY63aI/AAAAAAAAAaQ/VhaFFAAzH9E/s1600-h/image%5B15%5D.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="418" alt="image" src="http://lh6.ggpht.com/_bxrn2DJZgfo/TGPMcCYCh2I/AAAAAAAAAaU/mZ2FOTq9FDM/image_thumb%5B7%5D.png?imgmax=800" width="688" border="0" /></a> <br />Clicking Add will bring up the following dialog <br /><a href="http://lh3.ggpht.com/_bxrn2DJZgfo/TGPMeSQytlI/AAAAAAAAAaY/iLuR6UGWOF0/s1600-h/image%5B19%5D.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="194" alt="image" src="http://lh4.ggpht.com/_bxrn2DJZgfo/TGPMhqZKaoI/AAAAAAAAAac/fhIZ72q7pgg/image_thumb%5B9%5D.png?imgmax=800" width="347" border="0" /></a> <br />If you want the style to be applied on Site Collection level, then select the scope as Site, and Click OK will add feature/element files as shown below <br /><a href="http://lh5.ggpht.com/_bxrn2DJZgfo/TGPMkJzZ6KI/AAAAAAAAAag/3l5if5up26A/s1600-h/image%5B23%5D.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="289" alt="image" src="http://lh3.ggpht.com/_bxrn2DJZgfo/TGPMnXzGTMI/AAAAAAAAAak/igMr2cfDipU/image_thumb%5B11%5D.png?imgmax=800" width="290" border="0" /></a> <br />Now add your style file under the folder TestStyle as shown below <br /><a href="http://lh4.ggpht.com/_bxrn2DJZgfo/TGPMq3e1wgI/AAAAAAAAAao/XdRKjTcNG4Q/s1600-h/image%5B27%5D.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="289" alt="image" src="http://lh6.ggpht.com/_bxrn2DJZgfo/TGPMuFHKysI/AAAAAAAAAas/nwSiUvOlbAE/image_thumb%5B13%5D.png?imgmax=800" width="273" border="0" /></a> <br />Open feature.xml file and copy paste the following replacing the existing ElementManifests tag <br /><span style="color: blue"><</span><span style="color: #a31515">ElementManifests</span><span style="color: blue">> <br />        <</span><span style="color: #a31515">ElementManifest </span><span style="color: red">Location</span><span style="color: blue">=</span>"<span style="color: blue">elements.xml</span>"<span style="color: blue">/> <br />        <</span><span style="color: #a31515">ElementFile </span><span style="color: red">Location</span><span style="color: blue">=</span>"<span style="color: blue">MyStyle.css</span>" <span style="color: blue">/>         <br /></</span><span style="color: #a31515">ElementManifests</span><span style="color: blue">> <br /></span> <br />Open elements.xml file and copy paste the following replacing existing contents <br /> <pre class="code"><span style="color: blue"><?</span><span style="color: #a31515">xml </span><span style="color: red">version</span><span style="color: blue">=</span>"<span style="color: blue">1.0</span>" <span style="color: red">encoding</span><span style="color: blue">=</span>"<span style="color: blue">utf-8</span>" <span style="color: blue">?><br /><</span><span style="color: #a31515">Elements </span><span style="color: red">xmlns</span><span style="color: blue">=</span>"<span style="color: blue">http://schemas.microsoft.com/sharepoint/</span>"<span style="color: blue">><br /><</span><span style="color: #a31515">Module </span><span style="color: red">Name</span><span style="color: blue">=</span>"<span style="color: blue">New Style</span>" <span style="color: red">Url</span><span style="color: blue">=</span>"<span style="color: blue">Style Library</span>" <span style="color: red">Path</span><span style="color: blue">=</span>"" <span style="color: red">RootWebOnly</span><span style="color: blue">=</span>"<span style="color: blue">TRUE</span>" <span style="color: blue">><br /><</span><span style="color: #a31515">File </span><span style="color: red">Url</span><span style="color: blue">=</span>"<span style="color: blue">MyStyle.css</span>" <span style="color: red">Type</span><span style="color: blue">=</span>"<span style="color: blue">GhostableInLibrary</span>" <span style="color: red">IgnoreIfAlreadyExists</span><span style="color: blue">=</span>"<span style="color: blue">TRUE</span>"<span style="color: blue">></</span><span style="color: #a31515">File</span><span style="color: blue">><br /></</span><span style="color: #a31515">Module</span><span style="color: blue">><br /></span><span style="color: blue"></</span><span style="color: #a31515">Elements</span><span style="color: blue">></span></pre><br /><br /><br /><br /><br /><br />Now build the WSP by Right Clicking the project from Solution Explorer<br /><br /><br /><br /><br /><br /><br /><br /><br /><a href="http://lh3.ggpht.com/_bxrn2DJZgfo/TGPMwuX1mvI/AAAAAAAAAaw/xYhP07OFbFw/s1600-h/image%5B31%5D.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="414" alt="image" src="http://lh6.ggpht.com/_bxrn2DJZgfo/TGPM0fJZfII/AAAAAAAAAa0/z-M4YkL4ifE/image_thumb%5B15%5D.png?imgmax=800" width="460" border="0" /></a> <br /><br /><br /><br /><br /><br />Add the solution, and activate the feature.<br /><br /><br /><br /><br /><br /><br /><br /><br />Now, if you notice the feature was suppose to overwrite the file if it exists. But, if the file exists its not overwriting, even if we provide “IgnoreIfAlreadyExists” flag in elements.xml file. As a solution for this, we need to handle the FeatureActivated event and manually checkout the file and overwrite it and checkin the file. To achieve this we need to build a class library, which should go to GAC.<br /><br /><br /><br /><br /><br /><br /><br /><pre class="code"><span style="color: blue">using </span>System;<br /><span style="color: blue">using </span>System.Collections.Generic;<br /><span style="color: blue">using </span>System.Text;<br /><span style="color: blue">using </span>Microsoft.SharePoint;<br /><span style="color: blue">using </span>System.IO;<br /><span style="color: blue">using </span>System.Diagnostics;</pre><br /><br /><pre class="code"><br /><br /><span style="color: blue">namespace </span>UpdateStyleReceiver<br />{<br /><span style="color: blue">class </span><span style="color: #2b91af">UpdateStyle </span>: <span style="color: #2b91af">SPFeatureReceiver<br /></span>{<br /><span style="color: #2b91af">SPWeb </span>web = <span style="color: blue">null</span>;<br /><span style="color: blue">string </span>directoryPath = <span style="color: blue">null</span>;<br /><br /><span style="color: green">// copy to location<br /></span><span style="color: blue">string </span>url = <span style="color: blue">null</span>;<br /><br /><span style="color: blue">public override void </span>FeatureInstalled(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties) { }<br /><br /><span style="color: blue">public override void </span>FeatureUninstalling(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties) { }<br /><br /><span style="color: blue">public override void </span>FeatureActivated(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties)<br />{<br />WriteMessageToEventLog(<span style="color: #a31515">"Feature Activated"</span>);<br /><br /><span style="color: blue">try<br /></span>{<br />web = ((<span style="color: #2b91af">SPSite</span>)properties.Feature.Parent).RootWeb;<br /><br /><span style="color: green">// set the directory path on the file system where the feature was activated<br /></span>directoryPath = properties.Definition.RootDirectory;<br /><br /><span style="color: green">// run with elevated permissions so we can overwrite the file<br /></span><span style="color: #2b91af">SPSecurity</span>.RunWithElevatedPrivileges(OverwriteFile);<br />}<br /><span style="color: blue">catch </span>(<span style="color: #2b91af">Exception </span>ex)<br />{<br />WriteMessageToEventLog(ex.ToString());<br />}<br />}<br /><br /><span style="color: blue">public void </span>OverwriteFile()<br />{<br /><span style="color: green">// create a new site object so that elevation works properly<br /></span><span style="color: #2b91af">SPSite </span>site = <span style="color: blue">new </span><span style="color: #2b91af">SPSite</span>(web.Site.ID);<br /><br /><span style="color: green">// copy to location<br /></span><span style="color: blue">string </span>url = <span style="color: blue">null</span>;<br /><br /><span style="color: green">// get the url to the local file<br /></span><span style="color: blue">string</span>[] localFile = System.IO.<span style="color: #2b91af">Directory</span>.GetFiles(directoryPath, <span style="color: #a31515">"*.css"</span>, System.IO.<span style="color: #2b91af">SearchOption</span>.TopDirectoryOnly);<br /><br /><span style="color: green">// define a fstream object so we can read the contents of the file into a byte array<br /></span><span style="color: #2b91af">FileStream </span>fstream = <span style="color: #2b91af">File</span>.OpenRead(localFile[0]);<br /><br /><span style="color: blue">byte</span>[] contents = <span style="color: blue">new byte</span>[fstream.Length];<br />fstream.Read(contents, 0, (<span style="color: blue">int</span>)fstream.Length);<br />fstream.Close();<br /><br /><span style="color: green">// get a handle to the master page gallery<br /></span><span style="color: #2b91af">SPList </span>styleLibrary = site.OpenWeb().Lists[<span style="color: #a31515">"Style Library"</span>];<br /><br /><span style="color: green">// get a handle to the folder we want to upload the file to<br /></span><span style="color: #2b91af">SPFolder </span>editingMenuFolder = styleLibrary.RootFolder;<br /><br /><span style="color: #2b91af">SPFile </span>customQuickAccessFile = editingMenuFolder.Files[<span style="color: #a31515">"MyStyle.css"</span>];<br /><br /><span style="color: green">// build the destination copy url<br /></span>url = site.Url + <span style="color: #a31515">"/" </span>+ editingMenuFolder.Url + <span style="color: #a31515">"/"</span>;<br /><br /><span style="color: green">// check out the file, replace it with the modified one, and check it back in, publish and approve<br /></span>customQuickAccessFile.CheckOut();<br />customQuickAccessFile.CopyTo(url + <span style="color: #a31515">"MyStyle_Original.css"</span>, <span style="color: blue">true</span>);<br /><br />WriteMessageToEventLog(editingMenuFolder.ServerRelativeUrl);<br />WriteMessageToEventLog(editingMenuFolder.Url);<br /><br /><span style="color: green">// check in new file<br /></span>customQuickAccessFile = editingMenuFolder.Files.Add(url + <span style="color: #a31515">"MyStyle.css"</span>, contents, <span style="color: blue">true</span>);<br />customQuickAccessFile.CheckIn(<span style="color: #a31515">"File over-written by activiating the Published Page View Feature"</span>);<br />customQuickAccessFile.Publish(<span style="color: #a31515">"File published by activating the Published Page View Feature"</span>);<br />customQuickAccessFile.Approve(<span style="color: #a31515">"Approved by Published Page View Feature"</span>);<br /><br />site.Close();<br />}<br /><br /><span style="color: blue">public void </span>DeleteFile()<br />{<br /><span style="color: green">// create a new site object so that elevation works properly<br /></span><span style="color: #2b91af">SPSite </span>site = <span style="color: blue">new </span><span style="color: #2b91af">SPSite</span>(web.Site.ID);<br /><br /><span style="color: green">// get a handle to the master page gallery<br /></span><span style="color: #2b91af">SPList </span>styleLibrary = site.OpenWeb().Lists[<span style="color: #a31515">"Style Library"</span>];<br /><br /><span style="color: green">// get a handle to the folder we want to upload the file to<br /></span><span style="color: #2b91af">SPFolder </span>editingMenuFolder = styleLibrary.RootFolder;<br /><br /><span style="color: #2b91af">SPFile </span>customQuickAccessFile = editingMenuFolder.Files[<span style="color: #a31515">"MyStyle_Original.css"</span>];<br /><br /><span style="color: green">// build the destination copy url<br /></span>url = site.Url + <span style="color: #a31515">"/" </span>+ editingMenuFolder.Url + <span style="color: #a31515">"/"</span>;<br /><br />customQuickAccessFile.MoveTo(url + <span style="color: #a31515">"MyStyle.css"</span>, <span style="color: blue">true</span>);<br /><br />customQuickAccessFile.Publish(<span style="color: #a31515">"File published by activating the Published Page View Feature"</span>);<br />customQuickAccessFile.Approve(<span style="color: #a31515">"Approved by Published Page View Feature"</span>);<br /><br />site.Close();<br />}<br /><br /><span style="color: blue">public override void </span>FeatureDeactivating(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties) <br />{<br />WriteMessageToEventLog(<span style="color: #a31515">"Deactiviating Feature"</span>);<br /><br /><span style="color: blue">try<br /></span>{<br />web = ((<span style="color: #2b91af">SPSite</span>)properties.Feature.Parent).RootWeb;<br /><br /><span style="color: green">// run with elevated permissions so we can overwrite the file<br /></span><span style="color: #2b91af">SPSecurity</span>.RunWithElevatedPrivileges(DeleteFile);<br />}<br /><span style="color: blue">catch </span>(<span style="color: #2b91af">Exception </span>ex)<br />{<br />WriteMessageToEventLog(ex.ToString());<br />}<br />}<br /><br /><span style="color: blue">private void </span>WriteMessageToEventLog(<span style="color: blue">string </span>message)<br />{<br /><span style="color: #2b91af">SPSecurity</span>.RunWithElevatedPrivileges(<span style="color: blue">delegate</span>()<br />{<br /><span style="color: #2b91af">EventLog</span>.WriteEntry(<span style="color: #a31515">"Copy Feature Receiver"</span>, message);<br />});<br />}<br />}<br />}</pre><br /><br /><br />Modify the feature.xml as follows<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><pre class="code"><span style="color: blue"><?</span><span style="color: #a31515">xml </span><span style="color: red">version</span><span style="color: blue">=</span>"<span style="color: blue">1.0</span>" <span style="color: red">encoding</span><span style="color: blue">=</span>"<span style="color: blue">utf-8</span>"<span style="color: blue">?><br /><</span><span style="color: #a31515">Feature </span><span style="color: red">Id</span><span style="color: blue">=</span>"<span style="color: blue">20402429-a875-482d-a4c8-e8449f48d04c</span>"<br /><span style="color: red">Title</span><span style="color: blue">=</span>"<span style="color: blue">Update Style</span>"<br /><span style="color: red">Description</span><span style="color: blue">=</span>"<span style="color: blue">Description for Update Style</span>"<br /><span style="color: red">Version</span><span style="color: blue">=</span>"<span style="color: blue">12.0.0.0</span>"<br /><span style="color: red">Hidden</span><span style="color: blue">=</span>"<span style="color: blue">FALSE</span>"<br /><span style="color: red">Scope</span><span style="color: blue">=</span>"<span style="color: blue">Site</span>"<br /><span style="color: red">DefaultResourceFile</span><span style="color: blue">=</span>"<span style="color: blue">core</span>"<br /><span style="color: red">ReceiverAssembly</span><span style="color: blue">=</span>"<span style="color: blue">UpdateStyleReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7701372303d9a778</span>"<br /><span style="color: red">ReceiverClass</span><span style="color: blue">=</span>"<span style="color: blue"><span style="color: blue">UpdateStyleReceiver</span>.UpdateStyle</span>"<br /><br /><span style="color: red">xmlns</span><span style="color: blue">=</span>"<span style="color: blue">http://schemas.microsoft.com/sharepoint/</span>"<span style="color: blue">><br /><</span><span style="color: #a31515">ElementManifests</span><span style="color: blue">><br /><</span><span style="color: #a31515">ElementManifest </span><span style="color: red">Location</span><span style="color: blue">=</span>"<span style="color: blue">elements.xml</span>"<span style="color: blue">/><br /><</span><span style="color: #a31515">ElementFile </span><span style="color: red">Location</span><span style="color: blue">=</span>"<span style="color: blue">MyStyle.css</span>" <span style="color: blue">/><br /></span><span style="color: blue"> </</span><span style="color: #a31515">ElementManifests</span><span style="color: blue">><br /></</span><span style="color: #a31515">Feature</span><span style="color: blue">><br /></span></pre><br /><br /><br /><a href="http://11011.net/software/vspaste"></a> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com1tag:blogger.com,1999:blog-9164837514531450619.post-65063039491013693342010-05-12T23:14:00.001-07:002010-05-12T23:20:46.802-07:00Obfuscation with DotfuscatorFrom <a href="http://en.wikipedia.org/wiki/Obfuscated_code" target="_blank">wiki</a><br />
<br />
<div style="text-align: justify;"><b>Obfuscated code</b> is <a href="http://en.wikipedia.org/wiki/Source_code">source</a> or <a href="http://en.wikipedia.org/wiki/Machine_code">machine code</a> that has been made difficult to understand. Programmers may deliberately obfuscate code to conceal its purpose or its logic to prevent tampering, deter reverse engineering or as a <a href="http://en.wikipedia.org/wiki/Puzzle">puzzle</a> or recreational challenge for readers. It is a form of <b><a href="http://en.wikipedia.org/wiki/Security_through_obscurity">security through obscurity</a></b>. Programs known as <i>obfuscators</i> transform readable code into obfuscated code using various techniques that might induce anti-debugging, anti-decompilation and anti-disassembly mechanism.</div><div align="left"><br />
</div><div style="text-align: justify;">Managed code compilers (C#, VB.NET, etc) generate IL, not native assembly code. This IL tends to be consistently structured and fairly easy to reverse engineer. Most optimization happens when the IL is JIT-compiled into native code, not during compilation. This means it's pretty easy to take a compiled assembly and de-compile it into source code, using a tool such as <a href="http://www.aisto.com/roeder/dotnet/">Reflector</a>. While this is a non-issue for web scenarios where all the code resides on the server, it's a big issue for some client scenarios, especially ISV applications. These client applications may contain trade secrets or sensitive information in their algorithms, data structures, or data. This is where obfuscation tools come in. Visual Studio ships with the community edition of <a href="http://preemptive.com/products/dotfuscator/index.html">Dotfuscator</a>, a popular obfuscation package.</div><div style="text-align: justify;"><br />
</div><b><u>How To</u></b><br />
<br />
From Visual Studio 2010 Tools menu, click Dotfuscator Software Services as shown in the figure below.<br />
<br />
<a href="http://lh5.ggpht.com/_bxrn2DJZgfo/S-uYnnKnfMI/AAAAAAAAAYc/JJ55mHXA2ik/s1600-h/image%5B3%5D.png"><img alt="image" border="0" height="484" src="http://lh6.ggpht.com/_bxrn2DJZgfo/S-uYoo34bXI/AAAAAAAAAYg/EAm4AwFSpKQ/image_thumb%5B1%5D.png?imgmax=800" style="border-width: 0px; display: inline;" title="image" width="389" /></a> <br />
<br />
This will start the obfuscation service. Once you start building the application/library the following form will come up.<br />
<br />
<a href="http://lh4.ggpht.com/_bxrn2DJZgfo/S-uYplOO-2I/AAAAAAAAAYk/3TXU006kkY4/s1600-h/image%5B15%5D.png"><img alt="image" border="0" height="463" src="http://lh4.ggpht.com/_bxrn2DJZgfo/S-uYqVxDu7I/AAAAAAAAAYo/eDa5nZSoEuA/image_thumb%5B7%5D.png?imgmax=800" style="border: 0px none; display: inline;" title="image" width="644" /></a> <br />
<br />
Right click Dotfuscator1 tree node and select “Add Assemblies”<br />
<br />
<a href="http://lh4.ggpht.com/_bxrn2DJZgfo/S-uYrUBT6oI/AAAAAAAAAYs/cO12JOOM1_8/s1600-h/image%5B18%5D.png"><img alt="image" border="0" height="61" src="http://lh3.ggpht.com/_bxrn2DJZgfo/S-uYsKmXiVI/AAAAAAAAAYw/1S68KAeeKsM/image_thumb%5B8%5D.png?imgmax=800" style="border: 0px none; display: inline;" title="image" width="210" /></a> <br />
<br />
Browse the assembly you want to obfuscate.<br />
<br />
<a href="http://lh4.ggpht.com/_bxrn2DJZgfo/S-uYs1xJG8I/AAAAAAAAAY0/Aqq-wc92zmQ/s1600-h/image%5B22%5D.png"><img alt="image" border="0" height="480" src="http://lh3.ggpht.com/_bxrn2DJZgfo/S-uYt1PgAcI/AAAAAAAAAY4/MQlFZpZbu3o/image_thumb%5B10%5D.png?imgmax=800" style="border: 0px none; display: inline;" title="image" width="644" /></a> <br />
<br />
Once you have selected the assemblies, build it using the Build Menu. If there is no error, then the new output will be obfuscated.A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-20217085399737875902010-05-06T04:27:00.000-07:002010-05-06T04:28:12.387-07:00Microsoft Dektop Player<object id="silverlightControl" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"><param name="source" value="http://www.microsoft.com/click/desktopplayer/ClientBin/DesktopPlayer.xap" /><param name="enableHtmlAccess" value="true" /><param name="onError" value="onSilverlightError"/><param name="background" value="#0b1016"/><param name="minRuntimeVersion"value="3.0.40818.0"/><param name="autoUpgrade" value="true" /><param name="initParams" value="IPAddress=1.1 BLRSNRFWL01"/><div id="slTeaser"><div id="teaserText"><p id="slTxtHeader">This site has been built with Microsoft Silverlight. Microsoft Silverlight delivers a new generation of high-quality audio and video, engaging media experiences, and interactive applications for the Web.</p><p id="slTxtClick"><a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0">Click to install now!</a></p><br /><p id="slTxtSmall">By clicking "Click to install now!" you accept the <a href="http://www.microsoft.com/silverlight/get-started/install/license.aspx" style="text-decoration: underline" target="_blank">Silverlight License Agreement.</a></p></div></div></object>A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-9242704594035134022010-05-06T04:23:00.001-07:002010-05-06T04:23:42.343-07:00Internet Explorer 9: Test Drive<p align="justify">Microsoft has released the second platform preview edition of their upcoming web browser edition Internet Explorer 9. The second release is pretty much similar to the first one with no major changes in user interface. All the updates are in the engine powering the browser. Microsoft stated that they are working on improving the performance of various engines powering the browser (JavaScript and so on). </p> <p align="justify"><a title="http://ie.microsoft.com/testdrive/" href="http://ie.microsoft.com/testdrive/">http://ie.microsoft.com/testdrive/</a></p> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-72971474961532475052010-04-14T22:10:00.000-07:002010-04-14T22:10:01.626-07:00Let's become an MVPHere is your chance to prove the world how worthy you are. Lets showcase the efforts you have put to help communities.<br />
<br />
<a href="https://profile.microsoft.com/RegSysProfileCenter/wizard.aspx?wizid=b8b0dc5c-37e3-48e3-af75-e509830c8163&lcid=1033"><img src="http://img151.imageshack.us/img151/2098/69432785.png"/></a>A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-26331962314059602062010-04-11T01:34:00.001-07:002010-04-20T19:30:46.486-07:00LINQ Performance Benchmark<p>Yesterday I was reading “LINQ To Objects Using C# 4.0” by Troy Magennis and find a good LINQ usage example. I got curious about how the LINQ implementation will perform and thought of writing this article.</p><p>The example is related to an entity having 3 fields State, LastName and FirstName. The aim is to Group the objects by state and sort by LastName. For calculating the execution time I am using Stopwatch class in System.Diagnosis namespace.</p><pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Diagnostics;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>System.IO;
<span style="color: blue">namespace </span>ConsoleApplication2
{
<span style="color: blue">public class </span><span style="color: #2b91af">Customer
</span>{
<span style="color: blue">public string </span>State { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
<span style="color: blue">public string </span>LastName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
<span style="color: blue">public string </span>FirstName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
}
<span style="color: blue">class </span><span style="color: #2b91af">Program
</span>{
<span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args)
{
<span style="color: #2b91af">List</span><<span style="color: #2b91af">Customer</span>> custList = FillListAndGetList();
<span style="color: #2b91af">Stopwatch </span>sw = <span style="color: blue">new </span><span style="color: #2b91af">Stopwatch</span>();
sw.Start();
<span style="color: blue">#region </span>C# 2.0 Approach
<span style="color: gray">///</span><span style="color: green">Sorting by LastName
</span>custList.Sort(
<span style="color: blue">delegate</span>(<span style="color: #2b91af">Customer </span>c1, <span style="color: #2b91af">Customer </span>c2)
{
<span style="color: blue">if </span>(c1 != <span style="color: blue">null </span>&& c2 != <span style="color: blue">null</span>)
<span style="color: blue">return string</span>.Compare(c1.LastName, c2.FirstName);
<span style="color: blue">return </span>0;
});
<span style="color: gray">///</span><span style="color: green">Sort and Groupby State [SortedDictionary]
</span><span style="color: #2b91af">SortedDictionary</span><<span style="color: blue">string</span>, <span style="color: #2b91af">List</span><<span style="color: #2b91af">Customer</span>>>
sortedCust = <span style="color: blue">new </span><span style="color: #2b91af">SortedDictionary</span><<span style="color: blue">string</span>, <span style="color: #2b91af">List</span><<span style="color: #2b91af">Customer</span>>>();
<span style="color: blue">foreach </span>(<span style="color: #2b91af">Customer </span>c <span style="color: blue">in </span>custList)
{
<span style="color: blue">if </span>(!sortedCust.ContainsKey(c.State))
sortedCust.Add(c.State, <span style="color: blue">new </span><span style="color: #2b91af">List</span><<span style="color: #2b91af">Customer</span>>());
sortedCust[c.State].Add(c);
}
<span style="color: blue">foreach </span>(<span style="color: #2b91af">KeyValuePair</span><<span style="color: blue">string</span>, <span style="color: #2b91af">List</span><<span style="color: #2b91af">Customer</span>>>
pair <span style="color: blue">in </span>sortedCust)
{
<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"State : " </span>+ pair.Key);
<span style="color: blue">foreach </span>(<span style="color: #2b91af">Customer </span>c <span style="color: blue">in </span>pair.Value)
<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"Last Name : {0}" </span>+
<span style="color: #a31515">"FirstName : {1} "</span>, c.LastName,
c.FirstName);
}
<span style="color: blue">#endregion
</span>sw.Stop();
<span style="color: #2b91af">File</span>.WriteAllLines(<span style="color: #a31515">"C20"</span>, <span style="color: blue">new string</span>[] { sw.ElapsedTicks.ToString() });
custList = FillListAndGetList();
sw.Reset();
sw.Start();
<span style="color: blue">#region </span>LINQ Approach
<span style="color: blue">var </span>query = <span style="color: blue">from </span>c <span style="color: blue">in </span>custList
<span style="color: blue">orderby </span>c.State, c.LastName
<span style="color: blue">group </span>c <span style="color: blue">by </span>c.State;
<span style="color: blue">foreach </span>(<span style="color: blue">var </span>group <span style="color: blue">in </span>query)
{
<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"State : " </span>+ group.Key);
<span style="color: blue">foreach </span>(<span style="color: #2b91af">Customer </span>c <span style="color: blue">in </span>group)
<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"Last Name : {0}" </span>+
<span style="color: #a31515">"FirstName : {1} "</span>, c.LastName,
c.FirstName);
}
<span style="color: blue">#endregion
</span>sw.Stop();
<span style="color: #2b91af">File</span>.WriteAllLines(<span style="color: #a31515">"C20"</span>, <span style="color: blue">new string</span>[] { sw.ElapsedTicks.ToString() });
}
<span style="color: blue">private static </span><span style="color: #2b91af">List</span><<span style="color: #2b91af">Customer</span>> FillListAndGetList()
{
<span style="color: #2b91af">List</span><<span style="color: #2b91af">Customer</span>> custList = <span style="color: blue">new </span><span style="color: #2b91af">List</span><<span style="color: #2b91af">Customer</span>>();
<span style="color: blue">for </span>(<span style="color: blue">int </span>i = 0; i < 1000000; i++)
{
<span style="color: #2b91af">Customer </span>c = <span style="color: blue">new </span><span style="color: #2b91af">Customer</span>();
<span style="color: blue">if </span>(i % 2 == 0)
{
c.State = <span style="color: #a31515">"State2"</span>;
c. FirstName = <span style="color: #a31515">"FirstName2" </span>+ i.ToString();
c.LastName = <span style="color: #a31515">"LastName2" </span>+ i.ToString();
}
<span style="color: blue">else if </span>(i % 3 == 0)
{
c.State = <span style="color: #a31515">"State3"</span>;
c.FirstName = <span style="color: #a31515">"FirstName3" </span>+ i.ToString();
c.LastName = <span style="color: #a31515">"LastName3" </span>+ i.ToString();
}
<span style="color: blue">else if</span>(i % 7 == 0)
{
c.State = <span style="color: #a31515">"State7"</span>;
c.FirstName = <span style="color: #a31515">"FirstName7" </span>+ i.ToString();
c.LastName = <span style="color: #a31515">"LastName7" </span>+ i.ToString();
}
<span style="color: blue">else if </span>(i % 11 == 0)
{
c.State = <span style="color: #a31515">"State11"</span>;
c.FirstName = <span style="color: #a31515">"FirstName11" </span>+ i.ToString();
c.LastName = <span style="color: #a31515">"LastName11" </span>+ i.ToString();
}
<span style="color: blue">else
</span>{
c.State = <span style="color: #a31515">"OtherState"</span>;
c.FirstName = <span style="color: #a31515">"FirstName" </span>+ i.ToString();
c.LastName = <span style="color: #a31515">"LastName" </span>+ i.ToString();
}
custList.Add(c);
}
<span style="color: blue">return </span>custList;
}
}
}</pre><br />
Once I ran the application, I got the following result.<br />
<b>C# 2.0 version : 627373008</b><br />
<b>LINQ version : 692116906</b><br />
LINQ version has a slight performance problem, but compared to the chunk of code that has gone in 2.0 I think LINQ is far better.A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com2tag:blogger.com,1999:blog-9164837514531450619.post-88139680975552037942010-04-06T07:03:00.001-07:002010-04-06T07:03:51.576-07:00C# – Read, Insert, Update, Delete From SQL Database<p>Code snippets for reading, inserting, updating and deleting from SQL database.</p> <pre class="code"><span style="color: blue">static void </span>Read()<br />{<br /> <span style="color: blue">try<br /> </span>{<br /> <span style="color: blue">string </span>connectionString =<br /> <span style="color: #a31515">"server=.;" </span>+<br /> <span style="color: #a31515">"initial catalog=employee;" </span>+<br /> <span style="color: #a31515">"user id=sa;" </span>+<br /> <span style="color: #a31515">"password=sa123"</span>;<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlConnection </span>conn =<br /> <span style="color: blue">new </span><span style="color: #2b91af">SqlConnection</span>(connectionString))<br /> {<br /> conn.Open();<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlCommand </span>cmd =<br /> <span style="color: blue">new </span><span style="color: #2b91af">SqlCommand</span>(<span style="color: #a31515">"SELECT * FROM EmployeeDetails"</span>, conn))<br /> {<br /> <span style="color: #2b91af">SqlDataReader </span>reader = cmd.ExecuteReader();<br /><br /> <span style="color: blue">if </span>(reader.HasRows)<br /> {<br /> <span style="color: blue">while </span>(reader.Read())<br /> {<br /> <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"Id = "</span>, reader[<span style="color: #a31515">"Id"</span>]);<br /> <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"Name = "</span>, reader[<span style="color: #a31515">"Name"</span>]);<br /> <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"Address = "</span>, reader[<span style="color: #a31515">"Address"</span>]);<br /> }<br /> }<br /><br /> reader.Close();<br /> }<br /> }<br /> }<br /> <span style="color: blue">catch </span>(<span style="color: #2b91af">SqlException </span>ex)<br /> {<br /> <span style="color: green">//Log exception<br /> //Display Error message<br /> </span>}<br />}<br /><br /><span style="color: blue">static void </span>Insert()<br />{<br /> <span style="color: blue">try<br /> </span>{<br /> <span style="color: blue">string </span>connectionString =<br /> <span style="color: #a31515">"server=.;" </span>+<br /> <span style="color: #a31515">"initial catalog=employee;" </span>+<br /> <span style="color: #a31515">"user id=sa;" </span>+<br /> <span style="color: #a31515">"password=sa123"</span>;<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlConnection </span>conn =<br /> <span style="color: blue">new </span><span style="color: #2b91af">SqlConnection</span>(connectionString))<br /> {<br /> conn.Open();<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlCommand </span>cmd =<br /> <span style="color: blue">new </span><span style="color: #2b91af">SqlCommand</span>(<span style="color: #a31515">"INSERT INTO EmployeeDetails VALUES(" </span>+<br /> <span style="color: #a31515">"@Id, @Name, @Address)"</span>, conn))<br /> {<br /> cmd.Parameters.AddWithValue(<span style="color: #a31515">"@Id"</span>, 1);<br /> cmd.Parameters.AddWithValue(<span style="color: #a31515">"@Name"</span>, <span style="color: #a31515">"Amal Hashim"</span>);<br /> cmd.Parameters.AddWithValue(<span style="color: #a31515">"@Address"</span>, <span style="color: #a31515">"Bangalore"</span>);<br /><br /> <span style="color: blue">int </span>rows = cmd.ExecuteNonQuery();<br /><br /> <span style="color: green">//rows number of record got inserted<br /> </span>}<br /> }<br /> }<br /> <span style="color: blue">catch </span>(<span style="color: #2b91af">SqlException </span>ex)<br /> {<br /> <span style="color: green">//Log exception<br /> //Display Error message<br /> </span>}<br />}<br /><br /><span style="color: blue">static void </span>Update()<br />{<br /> <span style="color: blue">try<br /> </span>{<br /> <span style="color: blue">string </span>connectionString =<br /> <span style="color: #a31515">"server=.;" </span>+<br /> <span style="color: #a31515">"initial catalog=employee;" </span>+<br /> <span style="color: #a31515">"user id=sa;" </span>+<br /> <span style="color: #a31515">"password=sa123"</span>;<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlConnection </span>conn =<br /> <span style="color: blue">new </span><span style="color: #2b91af">SqlConnection</span>(connectionString))<br /> {<br /> conn.Open();<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlCommand </span>cmd =<br /> <span style="color: blue">new </span><span style="color: #2b91af">SqlCommand</span>(<span style="color: #a31515">"UPDATE EmployeeDetails SET Name=@NewName, Address=@NewAddress" </span>+<br /> <span style="color: #a31515">" WHERE Id=@Id"</span>, conn))<br /> {<br /> cmd.Parameters.AddWithValue(<span style="color: #a31515">"@Id"</span>, 1);<br /> cmd.Parameters.AddWithValue(<span style="color: #a31515">"@Name"</span>, <span style="color: #a31515">"Munna Hussain"</span>);<br /> cmd.Parameters.AddWithValue(<span style="color: #a31515">"@Address"</span>, <span style="color: #a31515">"Kerala"</span>);<br /><br /> <span style="color: blue">int </span>rows = cmd.ExecuteNonQuery();<br /><br /> <span style="color: green">//rows number of record got updated<br /> </span>}<br /> }<br /> }<br /> <span style="color: blue">catch </span>(<span style="color: #2b91af">SqlException </span>ex)<br /> {<br /> <span style="color: green">//Log exception<br /> //Display Error message<br /> </span>}<br />}<br /><br /><span style="color: blue">static void </span>Delete()<br />{<br /> <span style="color: blue">try<br /> </span>{<br /> <span style="color: blue">string </span>connectionString =<br /> <span style="color: #a31515">"server=.;" </span>+<br /> <span style="color: #a31515">"initial catalog=employee;" </span>+<br /> <span style="color: #a31515">"user id=sa;" </span>+<br /> <span style="color: #a31515">"password=sa123"</span>;<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlConnection </span>conn =<br /> <span style="color: blue">new </span><span style="color: #2b91af">SqlConnection</span>(connectionString))<br /> {<br /> conn.Open();<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlCommand </span>cmd =<br /> <span style="color: blue">new </span><span style="color: #2b91af">SqlCommand</span>(<span style="color: #a31515">"DELETE FROM EmployeeDetails " </span>+<br /> <span style="color: #a31515">"WHERE Id=@Id"</span>, conn))<br /> {<br /> cmd.Parameters.AddWithValue(<span style="color: #a31515">"@Id"</span>, 1);<br /> <br /> <span style="color: blue">int </span>rows = cmd.ExecuteNonQuery();<br /><br /> <span style="color: green">//rows number of record got deleted<br /> </span>}<br /> }<br /> }<br /> <span style="color: blue">catch </span>(<span style="color: #2b91af">SqlException </span>ex)<br /> {<br /> <span style="color: green">//Log exception<br /> //Display Error message<br /> </span>}<br />}</pre><br /><a href="http://11011.net/software/vspaste"></a> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com13tag:blogger.com,1999:blog-9164837514531450619.post-47569659282867980732010-04-04T11:05:00.001-07:002010-04-04T11:07:53.866-07:00C# | 3 Tier Architecture<p>In this article I am going to explain how easily we can build up a 3 layered application using .net framework and c#. I have an Employee database. For demonstration I have cut the scope by talking only to one table, EmployeeDetails.</p> <p>In 3 tier, we are dealing with </p> <p>1. Data Access Layer</p> <p>2. Business Layer</p> <p>3. Presentation Layer</p> Here is the code of my Data Access Layer <pre class="code"><span style="color: blue">static class </span><span style="color: #2b91af">DAL<br /></span>{<br /> <span style="color: blue">const string </span>ConnectionString = <span style="color: #a31515">"server=.;initial catalog=Employee;user id=sa;password=sa123"</span>;<br /><br /> <span style="color: blue">public static int </span>ExecuteNonQuery(<span style="color: blue">string </span>commandText, <span style="color: #2b91af">SqlParameter</span>[] parameters)<br /> {<br /> <span style="color: blue">try<br /> </span>{<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlConnection </span>conn = <span style="color: blue">new </span><span style="color: #2b91af">SqlConnection</span>(ConnectionString))<br /> {<br /> conn.Open();<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlCommand </span>cmd = <span style="color: blue">new </span><span style="color: #2b91af">SqlCommand</span>(commandText, conn))<br /> {<br /> <span style="color: blue">foreach </span>(<span style="color: #2b91af">SqlParameter </span>param <span style="color: blue">in </span>parameters)<br /> cmd.Parameters.Add(param);<br /><br /> <span style="color: blue">int </span>rowsAffected = cmd.ExecuteNonQuery();<br /><br /> <span style="color: blue">return </span>rowsAffected;<br /> }<br /> }<br /> }<br /> <span style="color: blue">catch<br /> </span>{<br /> <span style="color: blue">throw</span>;<br /> }<br /> }<br /><br /> <span style="color: blue">public static </span><span style="color: #2b91af">SqlDataReader </span>GetReader(<span style="color: blue">string </span>commandText, <span style="color: #2b91af">SqlParameter</span>[] parameters)<br /> {<br /> <span style="color: blue">try<br /> </span>{<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlConnection </span>conn = <span style="color: blue">new </span><span style="color: #2b91af">SqlConnection</span>(ConnectionString))<br /> {<br /> conn.Open();<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlCommand </span>cmd = <span style="color: blue">new </span><span style="color: #2b91af">SqlCommand</span>(commandText, conn))<br /> {<br /> <span style="color: blue">foreach </span>(<span style="color: #2b91af">SqlParameter </span>param <span style="color: blue">in </span>parameters)<br /> cmd.Parameters.Add(param);<br /><br /> <span style="color: #2b91af">SqlDataReader </span>reader = cmd.ExecuteReader();<br /><br /> <span style="color: blue">return </span>reader;<br /> }<br /> }<br /> }<br /> <span style="color: blue">catch<br /> </span>{<br /> <span style="color: blue">throw</span>;<br /> }<br /> }<br />}</pre><br />To resemble the table EmployeeDetails I have created the following entity class<br /><pre class="code"><span style="color: blue">class </span><span style="color: #2b91af">EmployeeEntity<br /></span>{<br /> <span style="color: blue">public int </span>Id { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br /> <span style="color: blue">public string </span>Name { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br /> <span style="color: blue">public string </span>Address { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br /><br /> <span style="color: blue">public override string </span>ToString()<br /> {<br /> <span style="color: #2b91af">StringBuilder </span>sb = <span style="color: blue">new </span><span style="color: #2b91af">StringBuilder</span>();<br /> sb.Append(<span style="color: #a31515">"Employee Name = "</span>).Append(<span style="color: blue">this</span>.Name);<br /> sb.Append(<span style="color: #a31515">" Have Id = "</span>).Append(<span style="color: blue">this</span>.Id);<br /> sb.Append(<span style="color: #a31515">" Lives In = "</span>).Append(<span style="color: blue">this</span>.Address);<br /><br /> <span style="color: blue">return </span>sb.ToString();<br /> }<br />}</pre><br /><p>Now comes the business layer</p><br /><pre class="code"><span style="color: blue">static class </span><span style="color: #2b91af">Employee<br /></span>{<br /> <span style="color: blue">public static int </span>AddEmployee(<span style="color: #2b91af">EmployeeEntity </span>e)<br /> {<br /> <span style="color: #2b91af">SqlParameter</span>[] param = <span style="color: blue">new </span><span style="color: #2b91af">SqlParameter</span>[3];<br /> param[0] = <span style="color: blue">new </span><span style="color: #2b91af">SqlParameter</span>(<span style="color: #a31515">"@Id"</span>, e.Id);<br /> param[1] = <span style="color: blue">new </span><span style="color: #2b91af">SqlParameter</span>(<span style="color: #a31515">"@Name"</span>, e.Name);<br /> param[2] = <span style="color: blue">new </span><span style="color: #2b91af">SqlParameter</span>(<span style="color: #a31515">"@Address"</span>, e.Address);<br /><br /> <span style="color: blue">return </span><span style="color: #2b91af">DAL</span>.ExecuteNonQuery(<span style="color: #a31515">"INSERT INTO EmployeeDetails VALUES(@Id, @Name, @Address)"</span>, param);<br /> }<br /><br /> <span style="color: blue">public static </span><span style="color: #2b91af">EmployeeEntity </span>GetEmployee(<span style="color: blue">int </span>id)<br /> {<br /> <span style="color: #2b91af">SqlParameter</span>[] param = <span style="color: blue">new </span><span style="color: #2b91af">SqlParameter</span>[1];<br /> param[0] = <span style="color: blue">new </span><span style="color: #2b91af">SqlParameter</span>(<span style="color: #a31515">"@Id"</span>, id);<br /><br /> <span style="color: #2b91af">SqlDataReader </span>reader = <span style="color: #2b91af">DAL</span>.GetReader(<span style="color: #a31515">"SELECT * FROM EmployeeDetails WHERE Id = @Id"</span>, param);<br /><br /> <span style="color: blue">if </span>(reader.HasRows)<br /> {<br /> <span style="color: #2b91af">EmployeeEntity </span>emp = <span style="color: blue">new </span><span style="color: #2b91af">EmployeeEntity</span>();<br /> <span style="color: blue">while </span>(reader.Read())<br /> {<br /> <span style="color: blue">int </span>temp = 0;<br /> <span style="color: blue">int</span>.TryParse(reader[<span style="color: #a31515">"Id"</span>].ToString(), <span style="color: blue">out </span>temp);<br /> emp.Id = temp;<br /> emp.Name = reader[<span style="color: #a31515">"Name"</span>].ToString();<br /> emp.Address = reader[<span style="color: #a31515">"Address"</span>].ToString();<br /><br /> <span style="color: blue">break</span>;<br /> }<br /> <span style="color: blue">return </span>emp;<br /><br /> }<br /> <span style="color: blue">else<br /> return null</span>;<br /> }<br />}</pre><br />Finally my presentation<br /><pre class="code"><span style="color: blue">class </span><span style="color: #2b91af">Program<br /></span>{<br /> <span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args)<br /> {<br /> <span style="color: #2b91af">Employee</span>.AddEmployee(<span style="color: blue">new </span><span style="color: #2b91af">EmployeeEntity</span>() { Id = 1, Name = <span style="color: #a31515">"Amal"</span>, Address = <span style="color: #a31515">"MyAddress" </span>});<br /> <span style="color: #2b91af">Employee</span>.AddEmployee(<span style="color: blue">new </span><span style="color: #2b91af">EmployeeEntity</span>() { Id = 2, Name = <span style="color: #a31515">"Hashim"</span>, Address = <span style="color: #a31515">"Hashim Address" </span>});<br /> <span style="color: #2b91af">Employee</span>.AddEmployee(<span style="color: blue">new </span><span style="color: #2b91af">EmployeeEntity</span>() { Id = 3, Name = <span style="color: #a31515">"Rooney"</span>, Address = <span style="color: #a31515">"Manchester" </span>});<br /><br /> <span style="color: #2b91af">EmployeeEntity </span>emp = <span style="color: #2b91af">Employee</span>.GetEmployee(1);<br /><br /> <span style="color: #2b91af">Console</span>.WriteLine(emp);<br /> }<br />}</pre><br />For better demonstration, you can think the presentation layer as a windows form application. With a 3 textboxes and one button. You can enter Id, Name and Address and use the button event for adding the data. A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-78216375814302579772010-04-04T08:00:00.001-07:002010-04-05T06:54:06.968-07:00Sql | Parameterized Queries<p>In msdn forums, a common question is regarding database retrieval and updation. To help such users I thought of bringing up this article. I want to give a concrete example, which will demonstrate the usage. To start with, I have a Employee database with a table named EmployeeDetails witht the following structure.</p> <p><a href="http://lh3.ggpht.com/_bxrn2DJZgfo/S7ipisvERGI/AAAAAAAAAXo/j_Fs_FHcKw0/s1600-h/image%5B2%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_bxrn2DJZgfo/S7ipkHGrkdI/AAAAAAAAAXs/d3rs7UHAzeg/image_thumb.png?imgmax=800" width="215" height="155" /></a> </p> <p>In the below code sample, I will show how to insert a record and get it back. To avoid the problems that can cause from SQL Injection, .Net framework has introduced Parameterized queries. Another way to get around this situation is using stored procedures.</p> <pre class="code"><span style="color: blue">using </span>System;<br /><span style="color: blue">using </span>System.Data.SqlClient;<br /><br /><span style="color: blue">namespace </span>ConsoleApplication2<br />{ <br /> <span style="color: blue">class </span><span style="color: #2b91af">Program<br /> </span>{<br /> <span style="color: blue">public static void </span>Main(<span style="color: blue">string</span>[] args)<br /> {<br /> <span style="color: blue">string </span>connectionString = <span style="color: #a31515">"server=.;initial catalog=employee;user id=sa;password=sa123"</span>;<br /><br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlConnection </span>conn = <span style="color: blue">new </span><span style="color: #2b91af">SqlConnection</span>(connectionString))<br /> {<br /> conn.Open();<br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlCommand </span>cmd = <span style="color: blue">new </span><span style="color: #2b91af">SqlCommand</span>(<span style="color: #a31515">"INSERT INTO" </span><span style="color: #a31515">+ "EmployeeDetails Values(@Id, @Name, @Address)"</span>, conn))<br /> {<br /> cmd.Parameters.Add(<span style="color: #a31515">"@Id"</span>, System.Data.<span style="color: #2b91af">SqlDbType</span>.Int);<br /> cmd.Parameters.Add(<span style="color: #a31515">"@Name"</span>, System.Data.<span style="color: #2b91af">SqlDbType</span>.VarChar);<br /> cmd.Parameters.Add(<span style="color: #a31515">"@Address"</span>, System.Data.<span style="color: #2b91af">SqlDbType</span>.VarChar);<br /><br /> cmd.Parameters[<span style="color: #a31515">"@Id"</span>].Value = 4;<br /> cmd.Parameters[<span style="color: #a31515">"@Name"</span>].Value = <span style="color: #a31515">"Rooney"</span>;<br /> cmd.Parameters[<span style="color: #a31515">"@Address"</span>].Value = <span style="color: #a31515">"Manchester United"</span>;<br /><br /> <span style="color: blue">int </span>rowsInserted = cmd.ExecuteNonQuery();<br /><br /> <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"Rows inserted = " </span>+ rowsInserted);<br /> }<br /><br /> <span style="color: blue">using </span>(<span style="color: #2b91af">SqlCommand </span>cmd = <span style="color: blue">new </span><span style="color: #2b91af">SqlCommand</span>(<span style="color: #a31515">"SELECT * FROM EmployeeDetails"</span>, conn))<br /> {<br /> <span style="color: #2b91af">SqlDataReader </span>reader = cmd.ExecuteReader();<br /> <span style="color: blue">if </span>(reader.HasRows)<br /> {<br /> <span style="color: blue">while </span>(reader.Read())<br /> {<br /> <span style="color: #2b91af">Console</span>.WriteLine(reader[<span style="color: #a31515">"Id"</span>].ToString() + <span style="color: #a31515">" " </span>+ reader[<span style="color: #a31515">"Name"</span>].ToString() + <span style="color: #a31515">" " </span>+ reader[<span style="color: #a31515">"Address"</span>].ToString());<br /> }<br /> }<br /> <span style="color: blue">else<br /> </span>{<br /> <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"No Rows"</span>);<br /> }<br /> }<br /> }<br /> }<br /> }<br />}</pre><br />Hope this was helpful.<br /><br /> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-63366872256067562322010-04-04T01:04:00.001-07:002010-04-04T01:09:17.530-07:00.Net Framework | Value Types<p>Value type holds data in the variable. They are stored in “Stack”. Because of this the performance is good and causes minimal overhead. Mainly there are three value types</p> <p>1. In built</p> <p>2. User defined(structs)</p> <p>3. Enums</p> <p>All the above are derived from System.Value</p> <table border="1" cellspacing="0" cellpadding="2" width="100%"><tbody> <tr> <td valign="top" width="133">Type</td> <td valign="top" width="133">Bytes Occupied</td> <td valign="top" width="133">Range</td> </tr> <tr> <td valign="top" width="133">System.SByte</td> <td valign="top" width="133">1</td> <td valign="top" width="133">-128 to 127</td> </tr> <tr> <td valign="top" width="133">System.Byte</td> <td valign="top" width="133">1</td> <td valign="top" width="133">0 to 255</td> </tr> <tr> <td valign="top" width="133">System.Int16</td> <td valign="top" width="133">2</td> <td valign="top" width="133">-32768 to 32767</td> </tr> <tr> <td valign="top" width="133">System.Int32</td> <td valign="top" width="133">4</td> <td valign="top" width="133">-2147483648 to 2147483647</td> </tr> <tr> <td valign="top" width="133">System.UInt32</td> <td valign="top" width="133">4</td> <td valign="top" width="133">0 to 4294967295</td> </tr> <tr> <td valign="top" width="133">System.Int64</td> <td valign="top" width="133">8</td> <td valign="top" width="133">-9223372036854775808 to 9223372036854775807</td> </tr> <tr> <td valign="top" width="133">System.Single</td> <td valign="top" width="133">4</td> <td valign="top" width="133">–3.402823E+38 to 3.402823E+38</td> </tr> <tr> <td valign="top" width="133">System.Double</td> <td valign="top" width="133">8</td> <td valign="top" width="133">-1.79769319486232E+308 to 1.79769319486232E+308</td> </tr> <tr> <td valign="top" width="133">System.Decimal</td> <td valign="top" width="133">16</td> <td valign="top" width="133">-79228162514264337593543950335 to 79228162514264337593543950335 </td> </tr> <tr> <td valign="top" width="133">System.Char</td> <td valign="top" width="133">2</td> <td valign="top" width="133"> </td> </tr> <tr> <td valign="top" width="133">System.Boolean</td> <td valign="top" width="133">4</td> <td valign="top" width="133"> </td> </tr> <tr> <td valign="top" width="133">System.DateTime</td> <td valign="top" width="133">8</td> <td valign="top" width="133">1/1/0001 12:00:00 AM to 12/31/9999 11:59:59 PM </td> </tr> <tr> <td valign="top" width="133">System.Boolean</td> <td valign="top" width="133">4</td> <td valign="top" width="133">True/False</td> </tr> <tr> <td valign="top" width="133">System.IntPtr</td> <td valign="top" width="133">Platform Dependent</td> <td valign="top" width="133"> </td> </tr> </tbody></table> <p><strong><u>Nullable Types</u></strong></p> <p>In some scenarios we might need to store the value null in the basic types. For doing that we can make the type nullable as shown below</p> <pre class="code"><span style="color: blue">bool</span>? isValid = <span style="color: blue">null</span>;<br /> <span style="color: green">//or<br /></span><span style="color: #2b91af">Nullable</span><<span style="color: blue">bool</span>> isSelected = <span style="color: blue">null</span>;</pre><br /><a href="http://11011.net/software/vspaste"></a><br /><p><strong><u>Structures or User Defined Types</u></strong></p><br /><p>Struct as mentioned is also a value type and is stored on the stack. Struct resembles class, but have several differences. Structures are composite types. They form a meaningful  data. The most common example give for structures is the Point type available in System.Drawing namespace. Each point can be represented using x and y coordinate and hence a structure can be formed as</p><br /><pre class="code"><span style="color: blue">struct </span><span style="color: #2b91af">Point<br /></span>{<br /> <span style="color: blue">public </span><span style="color: #2b91af">UInt32 </span>X;<br /> <span style="color: blue">public </span><span style="color: #2b91af">UInt32 </span>Y;<br />}</pre><br /><a href="http://11011.net/software/vspaste"></a><br /><br /><p>I have enhanced the Point structure as shown below</p><br /><br /><pre class="code"><span style="color: blue">using </span>System;<br /><span style="color: blue">using </span>System.Text;<br /><br /><span style="color: blue">namespace </span>ConsoleApplication2<br />{<br /> <span style="color: blue">struct </span><span style="color: #2b91af">Point<br /> </span>{<br /> <span style="color: blue">public </span><span style="color: #2b91af">UInt32 </span>X;<br /> <span style="color: blue">public </span><span style="color: #2b91af">UInt32 </span>Y;<br /><br /> <span style="color: blue">public </span>Point(<span style="color: blue">uint </span>x, <span style="color: blue">uint </span>y)<br /> {<br /> <span style="color: blue">this</span>.X = x;<br /> <span style="color: blue">this</span>.Y = y;<br /> }<br /><br /> <span style="color: blue">public static </span><span style="color: #2b91af">Point </span><span style="color: blue">operator </span>+(<span style="color: #2b91af">Point </span>p1, <span style="color: #2b91af">Point </span>p2)<br /> {<br /> <span style="color: #2b91af">Point </span>newPoint = <span style="color: blue">new </span><span style="color: #2b91af">Point</span>(p1.X + p2.X, p1.Y + p2.Y);<br /><br /> <span style="color: blue">return </span>newPoint;<br /> }<br /><br /> <span style="color: blue">public static </span><span style="color: #2b91af">Point </span><span style="color: blue">operator </span>-(<span style="color: #2b91af">Point </span>p1, <span style="color: #2b91af">Point </span>p2)<br /> {<br /> <span style="color: #2b91af">Point </span>newPoint = <span style="color: blue">new </span><span style="color: #2b91af">Point</span>(p1.X + p2.X, p1.Y + p2.Y);<br /><br /> <span style="color: blue">return </span>newPoint;<br /> }<br /><br /> <span style="color: blue">public override string </span>ToString()<br /> {<br /> <span style="color: #2b91af">StringBuilder </span>sb = <span style="color: blue">new </span><span style="color: #2b91af">StringBuilder</span>();<br /> sb.Append(<span style="color: #a31515">"The current point has X = "</span>).Append(<span style="color: blue">this</span>.X.ToString())<br /> .Append(<span style="color: #a31515">" and Y = "</span>).Append(<span style="color: blue">this</span>.Y.ToString());<br /> <span style="color: blue">return </span>sb.ToString();<br /> }<br /> }<br /> <br /> <span style="color: blue">class </span><span style="color: #2b91af">Program<br /> </span>{<br /> <span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args)<br /> {<br /> <span style="color: #2b91af">Point </span>p1 = <span style="color: blue">new </span><span style="color: #2b91af">Point</span>(10, 20);<br /> <span style="color: #2b91af">Point </span>p2 = <span style="color: blue">new </span><span style="color: #2b91af">Point</span>(20, 30);<br /><br /> p1 += p2;<br /><br /> <span style="color: #2b91af">Console</span>.WriteLine(p1);<br /> <br /> }<br /><br /> }<br />}</pre><br /><p>As you can see the structure has a parameterized constructor and can have methods. It also has overloaded operators. </p><br /><p><u>Note: We can’t use default constructors in Structure. Default constructor is used by the framework to initialize the fields in structure.</u></p><br /><p><u>Note: Always make use of a structure only if the entire data in it constitute less than 16 bytes.</u></p><br /><p><strong><u>Enumerations or Enum</u></strong></p><br /><p>Enums are grouped constants. They are introduced mainly to make the code readable.</p><br /><p>Lets extend the above application by adding an Enum Direction. Based on the direction the point will be moved.</p><br /><pre class="code"><span style="color: blue">using </span>System;<br /><span style="color: blue">using </span>System.Text;<br /><br /><span style="color: blue">namespace </span>ConsoleApplication2<br />{<br /> <span style="color: blue">struct </span><span style="color: #2b91af">Point<br /> </span>{<br /> <span style="color: blue">public </span><span style="color: #2b91af">UInt32 </span>X;<br /> <span style="color: blue">public </span><span style="color: #2b91af">UInt32 </span>Y;<br /><br /> <span style="color: blue">public </span>Point(<span style="color: blue">uint </span>x, <span style="color: blue">uint </span>y)<br /> {<br /> <span style="color: blue">this</span>.X = x;<br /> <span style="color: blue">this</span>.Y = y;<br /> }<br /><br /> <span style="color: blue">public static </span><span style="color: #2b91af">Point </span><span style="color: blue">operator </span>+(<span style="color: #2b91af">Point </span>p1, <span style="color: #2b91af">Point </span>p2)<br /> {<br /> <span style="color: #2b91af">Point </span>newPoint = <span style="color: blue">new </span><span style="color: #2b91af">Point</span>(p1.X + p2.X, p1.Y + p2.Y);<br /><br /> <span style="color: blue">return </span>newPoint;<br /> }<br /><br /> <span style="color: blue">public static </span><span style="color: #2b91af">Point </span><span style="color: blue">operator </span>-(<span style="color: #2b91af">Point </span>p1, <span style="color: #2b91af">Point </span>p2)<br /> {<br /> <span style="color: #2b91af">Point </span>newPoint = <span style="color: blue">new </span><span style="color: #2b91af">Point</span>(p1.X + p2.X, p1.Y + p2.Y);<br /><br /> <span style="color: blue">return </span>newPoint;<br /> }<br /><br /> <span style="color: blue">public override string </span>ToString()<br /> {<br /> <span style="color: #2b91af">StringBuilder </span>sb = <span style="color: blue">new </span><span style="color: #2b91af">StringBuilder</span>();<br /> sb.Append(<span style="color: #a31515">"The current point has X = "</span>).Append(<span style="color: blue">this</span>.X.ToString())<br /> .Append(<span style="color: #a31515">" and Y = "</span>).Append(<span style="color: blue">this</span>.Y.ToString());<br /> <span style="color: blue">return </span>sb.ToString();<br /> }<br /><br /> <span style="color: blue">public void </span>Move(<span style="color: #2b91af">Direction </span>d)<br /> {<br /> <span style="color: blue">switch </span>(d)<br /> {<br /> <span style="color: blue">case </span><span style="color: #2b91af">Direction</span>.Up:<br /> <span style="color: blue">if </span>(<span style="color: blue">this</span>.X > 0)<br /> <span style="color: blue">this</span>.X--;<br /> <span style="color: blue">break</span>;<br /> <span style="color: blue">case </span><span style="color: #2b91af">Direction</span>.Down:<br /> <span style="color: blue">if </span>(<span style="color: blue">this</span>.X < 1000)<br /> <span style="color: blue">this</span>.X++;<br /> <span style="color: blue">break</span>;<br /> <span style="color: blue">case </span><span style="color: #2b91af">Direction</span>.Left:<br /> <span style="color: blue">if </span>(<span style="color: blue">this</span>.Y > 0)<br /> <span style="color: blue">this</span>.Y--;<br /> <span style="color: blue">break</span>;<br /> <span style="color: blue">case </span><span style="color: #2b91af">Direction</span>.Right:<br /> <span style="color: blue">if </span>(<span style="color: blue">this</span>.Y < 1000)<br /> <span style="color: blue">this</span>.Y++;<br /> <span style="color: blue">break</span>;<br /> <span style="color: blue">default</span>:<br /> <span style="color: blue">break</span>;<br /> }<br /> }<br /> }<br /><br /> <span style="color: blue">enum </span><span style="color: #2b91af">Direction<br /> </span>{<br /> Up,<br /> Down,<br /> Left,<br /> Right<br /> }<br /> <br /> <span style="color: blue">class </span><span style="color: #2b91af">Program<br /> </span>{<br /> <span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args)<br /> {<br /> <span style="color: #2b91af">Point </span>p1 = <span style="color: blue">new </span><span style="color: #2b91af">Point</span>(0, 0);<br /> <span style="color: #2b91af">Console</span>.WriteLine(p1);<br /> p1.Move(<span style="color: #2b91af">Direction</span>.Left);<br /> <span style="color: #2b91af">Console</span>.WriteLine(p1);<br /> p1.Move(<span style="color: #2b91af">Direction</span>.Down);<br /> <span style="color: #2b91af">Console</span>.WriteLine(p1);<br /> p1.Move(<span style="color: #2b91af">Direction</span>.Left);<br /> <span style="color: #2b91af">Console</span>.WriteLine(p1);<br /> p1.Move(<span style="color: #2b91af">Direction</span>.Right);<br /> <span style="color: #2b91af">Console</span>.WriteLine(p1); <br /> }<br /><br /> }<br />}</pre> A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com2tag:blogger.com,1999:blog-9164837514531450619.post-90853505924630867322010-04-02T06:10:00.001-07:002010-04-02T06:13:26.252-07:00MVP – Visual C# – 2010<img src="http://adminframework.mvpaward.com/images/MVPLogo.jpg" /> <br /><br />Dear Amal Hashim,<br/><br/><br />Congratulations! We are pleased to present you with the 2010 Microsoft® MVP Award! This award is given to exceptional technical community leaders who actively share their high quality, real world expertise with others. We appreciate your outstanding contributions in Visual C# technical communities during the past year.<br/><br/><br />The Microsoft MVP Award provides us the unique opportunity to celebrate and honor your significant contributions and say "Thank you for your technical leadership."<br /><br/><br />Toby Richards<br />General Manager<br />Community & Online SupportA.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0tag:blogger.com,1999:blog-9164837514531450619.post-4926489902913467232010-03-29T09:57:00.001-07:002010-03-29T10:00:12.733-07:00SQL Server | Resetting Identity Column<div align="justify">Identity column’s are used when you want to have a serial number for each row that get inserted into the table. In other word, it will create a numeric sequence. We can make a column identity while creating the table. Check this simple query which will create an Employee table.</div><div align="justify"><br />
</div><pre class="code"><span style="color: blue;">CREATE TABLE </span><span style="color: teal;">EMPLOYEE</span><span style="color: grey;">(</span><span style="color: teal;">EmpID </span><span style="color: blue;">int identity</span><span style="color: grey;">(</span>1<span style="color: grey;">, </span>1<span style="color: grey;">), </span><span style="color: teal;">EmpName </span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">))</span></pre><br />
<div align="justify">The above statement will create a table named EMPLOYEE with two columns, EmpID and EmpName. EmpID is an identity column, the 1st record will have the EmpID as 1 and all subsequent records will have a value incremented by 1. So if you give identity(1,2), each record will have the EmpID as an increment of 2</div><br />
<div align="justify">At some instant we might require the identity value of the insert statement.</div><br />
<div align="justify"><span style="color: blue;">SELECT </span><span style="color: magenta;">@@IDENTITY</span></div><br />
If you want to insert a value into an identity column you can use the SET IDENTITY_INSERT statement.<br />
<div align="justify">Once you delete all the data from the table, the identity column won’t get reset to the initial value. For doing this we must use the following statement.</div><pre class="code"><span style="color: blue;">DBCC </span><span style="color: teal;">CHECKIDENT</span><span style="color: grey;">(</span><span style="color: red;">'Employee'</span><span style="color: grey;">, </span><span style="color: teal;">RESEED</span><span style="color: grey;">, </span>0<span style="color: grey;">)
</span></pre><br />
<span style="color: magenta;"><span style="color: #333333;">Another approach is to Truncate. This will delete all data as well as reset the Identity column.</span></span><br />
<br />
<pre class="code"><span style="color: blue;">TRUNCATE TABLE </span><span style="color: teal;">Employee
</span></pre>A.m.a.L Hashimhttp://www.blogger.com/profile/04600206670507170118noreply@blogger.com0