tag:blogger.com,1999:blog-7909563588100662672024-03-28T12:54:38.352+05:30Experience The CreativityASP MVC, C#, SharePoint, Android, Blogger Tips and SEO TipsSaranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.comBlogger47125tag:blogger.com,1999:blog-790956358810066267.post-42582174918134930712015-11-27T18:23:00.002+05:302015-12-09T18:39:41.686+05:30Export DataTable to Excel with Open XML in c#This code sample will illustrate how we can create Excel file from a data table in c#.<br />
You will need to add <a href="https://www.nuget.org/packages/documentformat.openxml/">DocumentFormat.OpenXml</a> and WindowsBase dll references to make it work.<br />
<pre class="brush: csharp">private static void WriteExcelFile(string outputPath, DataTable table)
{
using (SpreadsheetDocument document = SpreadsheetDocument.Create(fileToGenerate, SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
var sheetData = new SheetData();
worksheetPart.Worksheet = new Worksheet(sheetData);
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };
sheets.Append(sheet);
Row headerRow = new Row();
List<String> columns = new List<string>();
foreach (System.Data.DataColumn column in table.Columns)
{
columns.Add(column.ColumnName);
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach (DataRow dsrow in table.Rows)
{
Row newRow = new Row();
foreach (String col in columns)
{
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(dsrow[col].ToString());
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
workbookPart.Workbook.Save();
}
}
</pre>
Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com1tag:blogger.com,1999:blog-790956358810066267.post-57077210638257285062015-06-10T10:21:00.003+05:302015-06-10T10:41:50.393+05:30Configure StyleCop with MSBuild to treat Warnings as ErrorsWe can easily configure StyleCop with MSBuild to make warnings appear as errors with the help of StyleCop.MSBuild NuGet package.<br />
<br />
First go to Package Manager Console and install it with following command.
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9RpP7iHQBNne8YXb5qPAcY9AIoeIW7wcg8Kg0CjGGlTGGpiGLIW1zV7APSOS7n1qx5QhcsG_aRZ94Kij0f2NesrRN8tVMHlfUWe2hOLDj-yF1fagidW5HWB6lfC5NaogmHMHSdRCHJo4s/s1600/Untitled.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9RpP7iHQBNne8YXb5qPAcY9AIoeIW7wcg8Kg0CjGGlTGGpiGLIW1zV7APSOS7n1qx5QhcsG_aRZ94Kij0f2NesrRN8tVMHlfUWe2hOLDj-yF1fagidW5HWB6lfC5NaogmHMHSdRCHJo4s/s1600/Untitled.jpg" /></a></div>
<br />
<pre class="brush: xml;">Install-Package StyleCop.MSBuild
</pre>
Then unload your project and open the project file to modify it.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTd4PhL_gqup_6NIq0rFMRSNxPYPktYBKEJFAyVLQ68zgtTBaRjEKXIlbNaklvIWewG6iA5bAfahEKQKGe88RoQl6PA9gtaDiCNVghE9ngIoJM6GpuShGW6rHKVoVb1pId3SJJ1Z2o_OIv/s1600/Untitled.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTd4PhL_gqup_6NIq0rFMRSNxPYPktYBKEJFAyVLQ68zgtTBaRjEKXIlbNaklvIWewG6iA5bAfahEKQKGe88RoQl6PA9gtaDiCNVghE9ngIoJM6GpuShGW6rHKVoVb1pId3SJJ1Z2o_OIv/s1600/Untitled.jpg" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqEke-_zA5bjS_9Gqq_1hGFjuQr_vKseBC2NGLPcq4SPFlHEGEzR0Ke7mRYMtQOJqJvnDAD4IwvDttoimpzmRPAhiNmDTjd5_3GAhq5riSJMkwauhWW7AuU7Ci4JSpf2h_pbuCI-qHhoDy/s1600/Untitled1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqEke-_zA5bjS_9Gqq_1hGFjuQr_vKseBC2NGLPcq4SPFlHEGEzR0Ke7mRYMtQOJqJvnDAD4IwvDttoimpzmRPAhiNmDTjd5_3GAhq5riSJMkwauhWW7AuU7Ci4JSpf2h_pbuCI-qHhoDy/s1600/Untitled1.jpg" /></a></div>
<br />
Now you have to add "StyleCopTreatErrorsAsWarnings" property inside "PropertyGroup" tag as below.<br />
<pre class="brush: xml;highlight: [4]"><Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
</PropertyGroup>
<Error Condition="!Exists('..\packages\StyleCop.MSBuild.4.7.49.1\build\StyleCop.MSBuild.Targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\StyleCop.MSBuild.4.7.49.1\build\StyleCop.MSBuild.Targets'))" />
</Target>
</pre>
Almost done, but sometimes auto generated files gives errors when we build the project, to overcome that we need to modify "Settings.StyleCop" file.
<br />
<br />
Go to project folder and inside that you can find folder called "packages". It contains StyleCop files and inside "tools" folder you can find that setting file. You can open it with a test editor.
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9Mo1d8TUaK_gO8EHqpAh-Iqr63S4V1ro8zyI63CQ5862M0h4J635DAA4BWjsyklo-gwebedQZQxvOmJsI4YckPupBy7LXXY9N4byl5ot5w2joIA5PX9Av3y7ImQdwmCe4WRPrmYukuE83/s1600/Untitled.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9Mo1d8TUaK_gO8EHqpAh-Iqr63S4V1ro8zyI63CQ5862M0h4J635DAA4BWjsyklo-gwebedQZQxvOmJsI4YckPupBy7LXXY9N4byl5ot5w2joIA5PX9Av3y7ImQdwmCe4WRPrmYukuE83/s1600/Untitled.jpg" /></a></div>
<br />
Now you can add new value to GeneratedFileFilters as bellow to ignore those generated files.
<br />
<pre class="brush: xml;highlight: [7]"><Parser ParserId="StyleCop.CSharp.CsParser">
<ParserSettings>
<CollectionProperty Name="GeneratedFileFilters">
<Value>\.g\.cs$</Value>
<Value>\.generated\.cs$</Value>
<Value>\.g\.i\.cs$</Value>
<Value>TemporaryGeneratedFile_.*\.cs$</Value>
</CollectionProperty>
</ParserSettings>
</Parser>
</pre>
Now reload your project and try to build your project. You will be able to see that all the warnings appear as errors.
Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com0tag:blogger.com,1999:blog-790956358810066267.post-60125864272663546622015-04-22T14:29:00.001+05:302015-04-22T17:27:57.644+05:30MVC 5 with Unity for Dependency Injection<P>
Dependency Injection allows us to inject objects into a class, instead of constructing them internally. Unity is a dependency injection container that we can use for constructor, property, and method call injections.
</p>
<p>
You can get good idea about dependency injection by reading answers to <a target="_blank" href="http://stackoverflow.com/questions/130794/what-is-dependency-injection">this</a> stack-overflow question.
</P>
<p>
This tutorial is an step-by-step guide with screen-shots where you can learn how to use Unity with Asp.Net MVC5.
</p>
<p>
<b>I'm going to create sample web application for academic course listing website. There I have Institution and Course as main entities.</b>
</p>
<p>
I will not connect this sample application with a database and will illustrate with hard-coded data. Let's start...
</p>
<h3>
1. Create Sample Web Application
</h3>
<p>
In this example I'm using Visual Studio 2013 and let's start by creating sample MVC web application.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYH9R5kpf1dyp97oT4tmd0wbRQm0qozgDgJIAuU1QWkKpqUt2AdjDkEkLHtI4LFh1XtAULg6rdIe9Lguk7JY9eO95vtWPez4pude3-jN0g5X2se7soXzwlAD_2f5tmpgacTNgT_mamuwzG/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYH9R5kpf1dyp97oT4tmd0wbRQm0qozgDgJIAuU1QWkKpqUt2AdjDkEkLHtI4LFh1XtAULg6rdIe9Lguk7JY9eO95vtWPez4pude3-jN0g5X2se7soXzwlAD_2f5tmpgacTNgT_mamuwzG/s1600/Untitled.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioZ2mnPXFVkVLexI_XGdzsgl9NnkeCTmJeIXWNtJbAFzzT6qvVL5KK-_7xYdETR56SHyKkyzvjs6Jwe6FQh7Q8V-XLuj9Y9KHZkIoGPXIldwWCKcfxwxlD6iwP8zglWVeTtj4jM9hKmDCz/s1600/Untitled+(1).png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioZ2mnPXFVkVLexI_XGdzsgl9NnkeCTmJeIXWNtJbAFzzT6qvVL5KK-_7xYdETR56SHyKkyzvjs6Jwe6FQh7Q8V-XLuj9Y9KHZkIoGPXIldwWCKcfxwxlD6iwP8zglWVeTtj4jM9hKmDCz/s1600/Untitled+(1).png" /></a></div>
<p>
Check whether you can run the created web application and if there are no issues it should give following output.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheW-fPtlXj2DodZAwPa8P2Ns8pqccZSPqalsK5AbtdTVR2PEMXoM-d-4g083qEe_tW_cl8MEBFtCglSFBkXbVC-usWfZDIuN7A55TX5CfcakCF7B96lTBupoQs92wrntlN3O3sgc-ixVfZ/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheW-fPtlXj2DodZAwPa8P2Ns8pqccZSPqalsK5AbtdTVR2PEMXoM-d-4g083qEe_tW_cl8MEBFtCglSFBkXbVC-usWfZDIuN7A55TX5CfcakCF7B96lTBupoQs92wrntlN3O3sgc-ixVfZ/s1600/Capture.PNG" /></a></div>
<h3>
2. Install Unity.Mvc5 Library
</h3>
<p>
To install <a target="_blank" href="https://www.nuget.org/packages/Unity.Mvc5/">Unity.Mvc5 Library</a> using <a target="_blank" href="http://docs.nuget.org/">nuget</a>, we have to go to Package Manager Console and execute the following command.
</p>
<pre class="brush: css">
Install-Package Unity.Mvc5
</pre>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVADhuSkjYuU5vpnwfpwrb3dtWV0aCsZcJ53EAlbCsgo8beeI6qp0PIigz143MWDnSJg_2ku-e9cRo14c3BzojQKOmE4Ds8FVmVNzb7FjlYdeUchzy80jCU9jyAvLHOt9G-OC0vTjsIHx7/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVADhuSkjYuU5vpnwfpwrb3dtWV0aCsZcJ53EAlbCsgo8beeI6qp0PIigz143MWDnSJg_2ku-e9cRo14c3BzojQKOmE4Ds8FVmVNzb7FjlYdeUchzy80jCU9jyAvLHOt9G-OC0vTjsIHx7/s1600/Capture.PNG" /></a></div>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDBxmy7hPiC5aCZexIKRbOYaz-hg3NWV-AAWUyTPXZdIve1wmmntnHtJ3fu3yJACFXz-fhTzZQ4Tf4EroTvBfCfHMRKYyFesVHu30kb04TKox6pumLV-U8yc9NQnHohfuxpFtU1chXB5EW/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDBxmy7hPiC5aCZexIKRbOYaz-hg3NWV-AAWUyTPXZdIve1wmmntnHtJ3fu3yJACFXz-fhTzZQ4Tf4EroTvBfCfHMRKYyFesVHu30kb04TKox6pumLV-U8yc9NQnHohfuxpFtU1chXB5EW/s1600/Capture.PNG" /></a></div>
<p>Wait until it completes the installation.</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1NiCCdW48Iu_wEOm0nXigppHSfStBzD-Pkam_rk6cJJ6zNHNcONSROqyPMU7B-OppqFxcibtmvmNsLzyFNlmg_UiA61SZR81ib8_E2DI7L9JDJ0_3j13lFlJZXaytVB5kv6c-Spmp9SRU/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1NiCCdW48Iu_wEOm0nXigppHSfStBzD-Pkam_rk6cJJ6zNHNcONSROqyPMU7B-OppqFxcibtmvmNsLzyFNlmg_UiA61SZR81ib8_E2DI7L9JDJ0_3j13lFlJZXaytVB5kv6c-Spmp9SRU/s1600/Capture.PNG" /></a></div>
<p>
After completion if you check the Solution Explore, you can see newly added UnityConfig class inside App_Start folder. That's where we register all our components with the container.
</P>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7taIXV5N7PedL7I06g-EkGZxdHKOYyde-9mLfuKvzwMKt1qzIWJ_IparVRDAB9YLwpZdf_hJ6oblUcU_IMdiEaeZpGGstPirbrFf8kGM32tECYa5GFXR5zfyC5gvL2_OC_xJ-JokC_Vx7/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7taIXV5N7PedL7I06g-EkGZxdHKOYyde-9mLfuKvzwMKt1qzIWJ_IparVRDAB9YLwpZdf_hJ6oblUcU_IMdiEaeZpGGstPirbrFf8kGM32tECYa5GFXR5zfyC5gvL2_OC_xJ-JokC_Vx7/s1600/Capture.PNG" /></a></div>
<h3>
3. Update Sample Application
</h3>
<p>
Let's update our sample application now, first I'll add two another Class Library projects called Entities and Services. There Entities project holds all the entity (model) classes we are going to use and Services project will be used to put our service classes.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtdnG_fL79IOP4FGJc7nRtEHIo4k0BPtx81hn9pQkFyX0s22iXX-FNkhqsR3Xa2C45g3hyiP_Plutoq3tveDU_2MFkDlBgxMqvDupab4pgKY3F-CwKPPLg-go1dVhmoFbcr7tPGlZsL5iR/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtdnG_fL79IOP4FGJc7nRtEHIo4k0BPtx81hn9pQkFyX0s22iXX-FNkhqsR3Xa2C45g3hyiP_Plutoq3tveDU_2MFkDlBgxMqvDupab4pgKY3F-CwKPPLg-go1dVhmoFbcr7tPGlZsL5iR/s1600/Capture.PNG" /></a></div>
<p>
Then I'll add following classes to created Entity project.
</p>
<p>
Institution Class
</p>
<pre class="brush: csharp">
namespace Entities
{
public class Institution
{
public long InstitutionID { get; set; }
public string Name { get; set; }
public string Telephone { get; set; }
public string Address { get; set; }
}
}
</pre>
<p>
Course Class
</p>
<pre class="brush: csharp">
namespace Entities
{
public class Course
{
public long CourseID { get; set; }
public long InstitutionID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
</pre>
<p>
Next I will create the Services Class Library project and add reference to Entities project from Services project.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOI2PLACawdgepzzPC6HM_-ZtYcVyYW2lfy7PbCqFDhRYKYCrRDpAT20v7NUxOhL_8Lpi2oBJ_DWSXd8lClpgP5cVwG6VZQzjUkDPlJ0tRy83UrsxgESuFfQb9uNLGr-2cNsqXzZPbjuRz/s1600/Untitled+(1).png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOI2PLACawdgepzzPC6HM_-ZtYcVyYW2lfy7PbCqFDhRYKYCrRDpAT20v7NUxOhL_8Lpi2oBJ_DWSXd8lClpgP5cVwG6VZQzjUkDPlJ0tRy83UrsxgESuFfQb9uNLGr-2cNsqXzZPbjuRz/s1600/Untitled+(1).png" /></a></div>
<p>
Now I will create two service classes for institutions and courses. And also I will add relevant interfaces for those classes.
</p>
<p>
IInstitutionService Interface
</p>
<pre class="brush: csharp">
using Entities;
namespace Services
{
public interface IInstitutionService
{
Institution GetInstitutionByID(long institutionID);
}
}
</pre>
<p>
InstitutionService Class
</p>
<pre class="brush: csharp">
using Entities;
namespace Services
{
public class InstitutionService : IInstitutionService
{
public Institution GetInstitutionByID(long institutionID)
{
return new Institution { InstitutionID = institutionID, Name = "Sample Institution", Address = "Sample Address", Telephone = "0123456789" };
}
}
}
</pre>
<p>
ICourseService Interface
</p>
<pre class="brush: csharp">
using Entities;
namespace Services
{
public interface ICourseService
{
Course GetCourseByID(long courseID);
}
}
</pre>
<p>
CourseService Class
</p>
<pre class="brush: csharp">
using Entities;
namespace Services
{
public class CourseService : ICourseService
{
public Course GetCourseByID(long courseID)
{
return new Course { CourseID = courseID, Description = "Sample course description", InstitutionID = 1, Title = "Sample Course Title" };
}
}
}
</pre>
<p>
Your solution explore should looks like below now with all those projects and classes.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoMeKWUBJrdD0QWGSpByCNqCYaTVUvV3o1gaPWyXVct5leLlafbu79eL1TaAZ4HU0-weTYOfLaEwUi7Odm0SRXH2Z_mnubPMMuddbmuld1ZvvTkAs5ow7Z1SZEUjaccL5ZvtDl9OuK1UX-/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoMeKWUBJrdD0QWGSpByCNqCYaTVUvV3o1gaPWyXVct5leLlafbu79eL1TaAZ4HU0-weTYOfLaEwUi7Odm0SRXH2Z_mnubPMMuddbmuld1ZvvTkAs5ow7Z1SZEUjaccL5ZvtDl9OuK1UX-/s1600/Capture.PNG" /></a></div>
<p>
We need to add references to Entities and Service projects within Unity Web Application before we continue.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpgnT5xl6OoW40VhD_pRd1bhjSV6M-ryKFfwRAOUbUgSUNE6yb1273kqrisjq4OsQrOe5z1RLFVPujgMhyphenhyphen77i5QYjyy-rlMX6X-qdXWgelXNf1ExS4ZVJWP2WU89CUqxeYlUiMbZ8thhhW/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpgnT5xl6OoW40VhD_pRd1bhjSV6M-ryKFfwRAOUbUgSUNE6yb1273kqrisjq4OsQrOe5z1RLFVPujgMhyphenhyphen77i5QYjyy-rlMX6X-qdXWgelXNf1ExS4ZVJWP2WU89CUqxeYlUiMbZ8thhhW/s1600/Capture.PNG" /></a></div>
<h3>
4. Configure Unity
</h3>
<p>
Let's register our components with the unity container. There first we have to update our UnityConfig class as below.
</p>
<pre class="brush: csharp;highlight: [19,20]">
using Microsoft.Practices.Unity;
using Services;
using System.Web.Mvc;
using Unity.Mvc5;
namespace UnityWebApplication
{
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<ICourseService, CourseService>();
container.RegisterType<IInstitutionService, InstitutionService>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
}
</pre>
<p>
This RegisterComponents method can be called within the Application_Start method in Global.asax. We can update our Global.asax as below.
</p>
<pre class="brush: csharp;highlight: [11]">
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace UnityWebApplication
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
UnityConfig.RegisterComponents();
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
</pre>
<h3>
5. Continue with Sample Application
</h3>
<p>
Now we are going to display these institution and course information in our web application. I'll add new Empty Controller called Institution to our MVC application by right clicking Controllers folder.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhP-uZpfPHWkwS1PDt3XtYsII1cbK9SKoEk_oASnQiAbNdHigzor-34xLsFcTZ6v1c3-YbSK41S_ggFmr7hgzWM3j50DRDheIWO2iEb8-N6zPERpyA-6CaY9A8ZDa4YPOHu4xtEnLQh0Sdc/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhP-uZpfPHWkwS1PDt3XtYsII1cbK9SKoEk_oASnQiAbNdHigzor-34xLsFcTZ6v1c3-YbSK41S_ggFmr7hgzWM3j50DRDheIWO2iEb8-N6zPERpyA-6CaY9A8ZDa4YPOHu4xtEnLQh0Sdc/s1600/Capture.PNG" /></a></div>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0Ta_b2EzcVCFxHV74TCPd-ZLgyLRRJQQjKIVxqvHg8Rm47MBYlJgjYy3Aq4KmrZKpSLeeqYbhTxCT5lZePDPfO2r8-ES1tDldHZ5QiMnK8FSpiRYNiTMZh4CqU5lYwp_eo7Ghs8FgbjJH/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0Ta_b2EzcVCFxHV74TCPd-ZLgyLRRJQQjKIVxqvHg8Rm47MBYlJgjYy3Aq4KmrZKpSLeeqYbhTxCT5lZePDPfO2r8-ES1tDldHZ5QiMnK8FSpiRYNiTMZh4CqU5lYwp_eo7Ghs8FgbjJH/s1600/Capture.PNG" /></a></div>
<p>
Normally if we want to use our InstitutionService within the InstitutionController, we need to create instance of InstitutionService inside InstitutionController. Dependency injection is basically providing the objects that an object needs (here the InstitutionService) instead of having it construct them itself. I'm going to use constructor injection in this application and let the unity to create new instance of InstitutionService for InstitutionController. There we need to add constructor to our InstitutionController with parameter as below.
</p>
<pre class="brush: csharp">
using Services;
using System.Web.Mvc;
namespace UnityWebApplication.Controllers
{
public class InstitutionController : Controller
{
private IInstitutionService institutionService;
public InstitutionController(IInstitutionService institutionService)
{
this.institutionService = institutionService;
}
// GET: Institution
public ActionResult Index()
{
return View(this.institutionService.GetInstitutionByID(1));
}
}
}
</pre>
<p>
Let's add view to display our information, you can find folder called Institution inside Views folder and we will create new view called Index there.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizs21IVSRzQqdvN7tVVJ1cMH_zwidACft-24vvW3KgrWlhN_i4N2q9VmRMCOYpUxQBIhZiHk089r2vhs4X7P21yDnlvDk0psL_2PFLQq94a-1XCWfP9bdWx3W4rLJH2g4Z8YjoZ3kxsktW/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizs21IVSRzQqdvN7tVVJ1cMH_zwidACft-24vvW3KgrWlhN_i4N2q9VmRMCOYpUxQBIhZiHk089r2vhs4X7P21yDnlvDk0psL_2PFLQq94a-1XCWfP9bdWx3W4rLJH2g4Z8YjoZ3kxsktW/s1600/1.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZ7KInxgz5e3Uzyqh6h79bZyHpppBrUfEUBsZ91l7GVJ2CyDtpW8_5ewtnz5cJvlwuR5ydgo4-_O5a2uF2E6aYrj1cm0sUVgQIejTtVoqHwd_08KbqYVBRdcR4undXetRaRdVMrl1QLlwR/s1600/3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZ7KInxgz5e3Uzyqh6h79bZyHpppBrUfEUBsZ91l7GVJ2CyDtpW8_5ewtnz5cJvlwuR5ydgo4-_O5a2uF2E6aYrj1cm0sUVgQIejTtVoqHwd_08KbqYVBRdcR4undXetRaRdVMrl1QLlwR/s1600/3.PNG" /></a></div>
<p>
Now modify your view code as below to display institution information.
</p>
<pre class="brush: csharp">
@using Entities;
@model Institution
@{
ViewBag.Title = "Institution";
}
<hr />
Institution ID : @(Model.InstitutionID)
<hr />
Institution Name : @(Model.Name)
<hr />
Institution Address : @(Model.Address)
<hr />
Institution Telephone : @(Model.Telephone)
</pre>
<p>
It's done, you can run your application and try /Institution/Index. You should get following output.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUBllyX9c-BnCZh3RA1MpXUbMygRyJxU_7yDtg07UwWkaDXk16P3qPtn7KnuA0Z110PTac2zd7lE6OMoLGKsesfvEmzSIEMBBDYXrO86luSF7DBVIhPfWloJjuKp7kj65bjVoZd3p2GZX8/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUBllyX9c-BnCZh3RA1MpXUbMygRyJxU_7yDtg07UwWkaDXk16P3qPtn7KnuA0Z110PTac2zd7lE6OMoLGKsesfvEmzSIEMBBDYXrO86luSF7DBVIhPfWloJjuKp7kj65bjVoZd3p2GZX8/s1600/Capture.PNG" /></a></div>
<h3>
6. Move Unity Configuration to Separate File
</h3>
<p>
We can move unity configuration to separate file which enable us to do modifications without recompiling existing application. We can do this by adding config section to web.config file as below.
</p>
<pre class="brush: xml;highlight: [3,7]">
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<unity configSource="unity.config" />
<connectionStrings>
</pre>
<p>
unity.config
</p>
<pre class="brush: xml">
<?xml version="1.0" encoding="utf-8" ?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container>
<register type="Services.IInstitutionService, Services" mapTo="Services.InstitutionService, Services" />
<register type="Services.ICourseService, Services" mapTo="Services.CourseService, Services" />
</container>
</unity>
</pre>
<p>
We need to change the UnityConfig to load the configuration from given file, modified code can be found below.
</p>
<pre class="brush: csharp;highlight: [20,21,23]">
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Services;
using System.Web.Mvc;
using Unity.Mvc5;
namespace UnityWebApplication
{
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
//container.RegisterType<ICourseService, CourseService>();
//container.RegisterType<IInstitutionService, InstitutionService>();
container.LoadConfiguration();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
}
</pre>
<a target="_blank" href="https://www.dropbox.com/s/e218jm4ss6pmzpr/unity_web_application.zip?dl=0">Download Source</a> (Password:sara)
<p>
You can download the source code of sample application using above link. I have added some additional classes/methods to use InstitutionService inside CourseService. You can go through the code and will be able to understand easily.
</p>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com11tag:blogger.com,1999:blog-790956358810066267.post-50596480788559105362014-01-22T17:56:00.002+05:302015-04-22T00:59:00.515+05:30Pass Complex JavaScript Object to MVC Controller<p>
In this tutorial I'm going to illustrate how we can pass complex javascript objects to MVC controller.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVDiinEn41vNKbETxzuQrhZalX-SVKpmJsurYa7nW9l_6T5XaDiIqCTrI7N79AAWvYTO7TFE_mx-eT5i4baeotL-ltiwzVlMPD0hEt1IvlUxzQQ1cqezOGYJWwAuIbOztnINq-rh3J07Vd/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVDiinEn41vNKbETxzuQrhZalX-SVKpmJsurYa7nW9l_6T5XaDiIqCTrI7N79AAWvYTO7TFE_mx-eT5i4baeotL-ltiwzVlMPD0hEt1IvlUxzQQ1cqezOGYJWwAuIbOztnINq-rh3J07Vd/s1600/Untitled.png" /></a></div>
<p>
I have two Model objects called Project and Employee as below.
</p>
<pre class="brush: csharp;">
public class Project
{
public long ProjectID { get; set; }
public string ProjectName { get; set; }
}
public class Employee
{
public string Code { get; set; }
public string Name { get; set; }
}
</pre>
<p>
And I have view-model called "ProjectViewModel" which contains above models as below.
</p>
<pre class="brush: csharp;">
public class ProjectViewModel
{
public Project Project { get; set; }
public IList<Employee> EmployeeList { get; set; }
}
</pre>
Now I'm going to pass instance of ProjectViewModel and an Employee object to my MVC controller. Here is the controller method.
<pre class="brush: csharp;">
[HttpPost]
public ActionResult Create(ProjectViewModel projectViewModel, Employee anotherEmployee)
{
return Content("done");
}
</pre>
This is the JavaScript code that we can use to call the controller method.
<pre class="brush: js;">
var projectViewModel = {};
var project = {};
project.ProjectID = 1234;
project.ProjectName = "Test Project";
projectViewModel.Project = project;
var employeeList = [];
var employee1 = {};
employee1.Code = "1111";
employee1.Name = "Saranga";
var employee2 = {};
employee2.Code = "2222";
employee2.Name = "Rathnayaka";
employeeList.push(employee1);
employeeList.push(employee2);
projectViewModel.EmployeeList = employeeList;
var employee3 = {};
employee3.Code = "3333";
employee3.Name = "Test User";
$.ajax(
{
url: 'Project/Create',
data: JSON.stringify({projectViewModel : projectViewModel, anotherEmployee : employee3}),
contentType: 'application/json',
dataType: 'json',
type: 'POST',
success: function (data) {
alert("success");
},
error: function () { alert('error'); }
});
</pre>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com0tag:blogger.com,1999:blog-790956358810066267.post-81783939757766114462011-10-25T00:04:00.002+05:302015-04-22T00:59:36.261+05:30Android Web Service Access Tutorial<p>
In this post I'm going to illustrate how we can access web service in Android using <a href="http://code.google.com/p/ksoap2-android/" target="_blank">ksoap2-android project</a> that provides a lightweight and efficient SOAP library for the Android platform.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMh3Wn5Q-z3BiEcK_XRVORm034xyS5HoAx55Eb_Z0cuP2cCLdvWzvGQEYPQwYlySE0NgPbMmVx5NMQUMWQV0Qry7vawFESFd3zI0lXQT52iHAm-WfLl_-cHFKV-Q0jRwCNkFhseCImOtBA/s1600/untitled.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMh3Wn5Q-z3BiEcK_XRVORm034xyS5HoAx55Eb_Z0cuP2cCLdvWzvGQEYPQwYlySE0NgPbMmVx5NMQUMWQV0Qry7vawFESFd3zI0lXQT52iHAm-WfLl_-cHFKV-Q0jRwCNkFhseCImOtBA/s1600/untitled.JPG" /></a></div>
<p>
You can download the jar file from following link;
</p>
<p>
<a href="http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.8/ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar">http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.8/ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar</a></p>
<p>
Here is the sample code that illustrate SOAP web service call. I have access a live web service <a href="http://www.webservicex.net/ConvertWeight.asmx" target="_blank">ConvertWeight</a> from <a href="http://www.webservicex.net/" target="_blank">http://www.webserviceX.NET/</a> which convert weight from one unit to another. I have set "<i style="color: #783f04; font-family: "Courier New",Courier,monospace;">envelope.dotNet = true;</i>" in Line 53 since I'm accessing .NET web service. You can comment that line if your web service is not .NET one.
</p>
<pre class="brush: java;highlight: [53]">
package com.sencide;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class AndroidWebService extends Activity {
private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/ConvertWeight.asmx";
private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
private final String METHOD_NAME = "ConvertWeight";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String weight = "3700";
String fromUnit = "Grams";
String toUnit = "Kilograms";
PropertyInfo weightProp =new PropertyInfo();
weightProp.setName("Weight");
weightProp.setValue(weight);
weightProp.setType(double.class);
request.addProperty(weightProp);
PropertyInfo fromProp =new PropertyInfo();
fromProp.setName("FromUnit");
fromProp.setValue(fromUnit);
fromProp.setType(String.class);
request.addProperty(fromProp);
PropertyInfo toProp =new PropertyInfo();
toProp.setName("ToUnit");
toProp.setValue(toUnit);
toProp.setType(String.class);
request.addProperty(toProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.i("myApp", response.toString());
TextView tv = new TextView(this);
tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);
setContentView(tv);
} catch (Exception e) {
e.printStackTrace();
}
}
}
</pre>
<p>
You have to add INTERNET permission to AndroidManifest.xml as follows;
</p>
<pre class="brush: xml;highlight: [18]"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sencide"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidWebService"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
</pre>
<p>
You can <a href="https://www.dropbox.com/s/oczosizv6ket51o/android_web_service.zip?dl=0" target="_blank">download the source code of above project (Password:sara).</a>
</p>
<p>
I have updated my previous code in the post <a href="http://sarangasl.blogspot.com/2011/06/android-login-screen-using-httpclient.html" target="_blank">Android Login Screen Using HttpClient</a> to authenticate users from web service. There I have illustrated how you can start new activity if the login is successful.
</p>
<p>
You can download the <a href="https://www.dropbox.com/s/rtuyzl53tjyqw6c/android_login_web_service.zip?dl=0" target="_blank">updated source code with sample .NET web service application (Password:sara).</a></p>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com125tag:blogger.com,1999:blog-790956358810066267.post-62083190425659121462011-06-21T23:35:00.004+05:302015-04-22T01:06:17.865+05:30Android Login Screen Using HttpClient<p>
I have updated this code to use web service and open new screen if the login is successful in my post <a href="http://sarangasl.blogspot.com/2011/10/android-web-service-access-tutorial.html" target="_blank">Android Web Service Access Tutorial</a>.</p>
<p>
In Android we can use HTTP POST request with org.apache.http.client.HttpClient to post data to a URL. This sample project illustrate how we can post data to a URL and get the response.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV67bSFnD6OMihAfTESd_nu07a-D2fk_se9e4BwMUfqlqGxzVD4YjxBvyiOWS-JOgJ4WgLt1vTeaV7QetDnqlWD0vzTHNm8Te0DZluf5pmhf_8jinKvXFnlqbbEw2iczOp1leOkhjjBPY_/s1600/Capture.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV67bSFnD6OMihAfTESd_nu07a-D2fk_se9e4BwMUfqlqGxzVD4YjxBvyiOWS-JOgJ4WgLt1vTeaV7QetDnqlWD0vzTHNm8Te0DZluf5pmhf_8jinKvXFnlqbbEw2iczOp1leOkhjjBPY_/s1600/Capture.JPG" /></a></div>
<br />
<pre class="brush: java">
package com.sencide;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidLogin extends Activity implements OnClickListener {
Button ok,back,exit;
TextView result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Login button clicked
ok = (Button)findViewById(R.id.btn_login);
ok.setOnClickListener(this);
result = (TextView)findViewById(R.id.lbl_result);
}
public void postLoginData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
/* login.php returns true if username and password is equal to saranga */
HttpPost httppost = new HttpPost("http://www.sencide.com/blog/login.php");
try {
// Add user name and password
EditText uname = (EditText)findViewById(R.id.txt_username);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.txt_password);
String password = pword.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("SENCIDE", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("SENCIDE", str);
if(str.toString().equalsIgnoreCase("true"))
{
Log.w("SENCIDE", "TRUE");
result.setText("Login successful");
}else
{
Log.w("SENCIDE", "FALSE");
result.setText(str);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
@Override
public void onClick(View view) {
if(view == ok){
postLoginData();
}
}
}
</pre>
<p>
Following code is the content of mail.xml file that is used in the above code.</p>
<pre class="brush: xml"><?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText android:layout_width="150px" android:layout_height="wrap_content" android:textSize="18sp" android:id="@+id/txt_username" android:layout_y="132dip" android:layout_x="128dip"></EditText>
<EditText android:layout_width="150px" android:layout_height="wrap_content" android:textSize="18sp" android:password="true" android:id="@+id/txt_password" android:layout_x="128dip" android:layout_y="192dip"></EditText>
<Button android:layout_width="100px" android:layout_height="wrap_content" android:id="@+id/btn_login" android:layout_x="178dip" android:layout_y="252dip" android:text="Login"></Button>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lbl_username" android:text="User Name" android:layout_x="37dip" android:layout_y="150dip"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lbl_password" android:text="Password" android:layout_y="207dip" android:layout_x="50dip"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lbl_top" android:textSize="16sp" android:typeface="sans" android:text="Please Loggin First" android:layout_x="29dip" android:layout_y="94dip"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_y="312dip" android:layout_x="50dip" android:id="@+id/lbl_result"></TextView>
</AbsoluteLayout>
</pre>Following code is the content of AndroidManifest.xml file that is used in this project. There note that I have add the line <uses-permission android:name= "android.permission.INTERNET".<br />
<pre class="brush: xml"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sencide"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidLogin"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</pre>
<p>You can <a href="https://www.dropbox.com/s/szr0jun40nl9t1i/android_login.zip?dl=0" target="_blank">download the source code of above project (Password:sara)</a>.</p>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com96tag:blogger.com,1999:blog-790956358810066267.post-68822168972494214442011-06-08T12:00:00.002+05:302015-04-22T01:22:22.101+05:30SQL Server Authentication enabling using Microsoft SQL Server Management Studio<p>
If you install the Microsoft SQL Server using Windows Authentication mode, the "sa" account is disabled by default. So if you plan to use SQL Server Authentication, you have to enable the "sa" account. This tutorial tells you how to enable the "sa" account.
</p>
<p>
1. First, Login to the SQL Server Management Studio using Windows Authentication. Right-click on the database instance, and go to Properties.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPAuwroviABm2172F5Kt23q429LW99qY48hdfNbge9bWJshtLleOvo-qKm-or6B1ckiMv-MvitJheuhD9u8tUTolO0YdMgy1S2JaGr5YUaX1SANbFLN05lhuP7GBaL_VC1MlAl9LpNrIg/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPAuwroviABm2172F5Kt23q429LW99qY48hdfNbge9bWJshtLleOvo-qKm-or6B1ckiMv-MvitJheuhD9u8tUTolO0YdMgy1S2JaGr5YUaX1SANbFLN05lhuP7GBaL_VC1MlAl9LpNrIg/s1600/2.jpg" /></a></div>
<p>
2. Then on Properties page, click on Security and select SQL Server and Windows Authentication mode, and click on OK to close the Server Properties page.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgariRqCfMNFE7-3qNjdMZRueiBaEUxbttm9RCtlWbkQQylKqsO6FiZ_Au8R17C3eGj-IVjnyWbEOHCSIebDA_3I1h-n7xRJjvUWw5w1ZAmouOyHjPRvV5uEDgjB10bkg9iiFPshd32Kek/s1600/3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgariRqCfMNFE7-3qNjdMZRueiBaEUxbttm9RCtlWbkQQylKqsO6FiZ_Au8R17C3eGj-IVjnyWbEOHCSIebDA_3I1h-n7xRJjvUWw5w1ZAmouOyHjPRvV5uEDgjB10bkg9iiFPshd32Kek/s1600/3.jpg" /></a></div>
<p>
3. Now you will get dialog box saying that you should restart your SQL Server to take the changes take effect. Is is not done yet, you have to done one more thing to enable the "sa" login.
</p>
<p>
4. Now expand Security folder and go to Logins. You can see the "sa" account is disabled when you install SQL Server using Windows Authentication mode.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmk-lHl62BFsmrG58kRczDx69eSuxTiutjx8dShW56EykqVkQ6sDsbWr8D2tBXOt18XlmJzZZU-VKmqS5Eg6N1_iolIqwQEqzZErNMUir4CVKTWfxHyGT3UkypZGbyO98dXCg_ftAu5m4/s1600/4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmk-lHl62BFsmrG58kRczDx69eSuxTiutjx8dShW56EykqVkQ6sDsbWr8D2tBXOt18XlmJzZZU-VKmqS5Eg6N1_iolIqwQEqzZErNMUir4CVKTWfxHyGT3UkypZGbyO98dXCg_ftAu5m4/s1600/4.jpg" /></a></div>
<p>
5. Then right-click on the "sa" account and go to Login Properties. There you can set a password for the "sa" account.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2QSofTeVaChVsrMR9T5y9hkA0ylH9UHyduT_c9D1KPkXwe2zJfTt7RXc2wUEB0allLdiPfiROjF3uHiHlwUQBQCuQ5DEIVf250be12Q8iINPdZfOTBjdN8cmfvLJYJyWzHROrN_-60ug/s1600/5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2QSofTeVaChVsrMR9T5y9hkA0ylH9UHyduT_c9D1KPkXwe2zJfTt7RXc2wUEB0allLdiPfiROjF3uHiHlwUQBQCuQ5DEIVf250be12Q8iINPdZfOTBjdN8cmfvLJYJyWzHROrN_-60ug/s1600/5.jpg" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFch7BGSIVUmqupZiGMYuE99zaI2T-k3AUySgiWAMok3Dz7-fJ_J6eRLhw5acxULaYkmflZ1pYqNQkdezZoVScZIPyukvyDk-O0UF7-Antpo1fvy73ITORGMDXvSkRb-N1BAGoaUQAU1M/s1600/5.1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFch7BGSIVUmqupZiGMYuE99zaI2T-k3AUySgiWAMok3Dz7-fJ_J6eRLhw5acxULaYkmflZ1pYqNQkdezZoVScZIPyukvyDk-O0UF7-Antpo1fvy73ITORGMDXvSkRb-N1BAGoaUQAU1M/s1600/5.1.jpg" /></a></div>
<p>
6. Click on the Status page. There you can see the "sa" account is disabled by default. Click on the Enabled button to enable it. Then click on Ok to close the "sa" Login Properties.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKg1l6K9d1DzU_yViQp1njpgFGY8jyKRqlZ-6w2VZSHLU2jAA2-tHv9XyIcGL05RrqtrPc9oxPQAOq0G-x0-yJc48gnBj2bJqVx-n-TbxTwlrGAZMMb1v6msyotp0RacAjOI5UMXdpJXk/s1600/6.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKg1l6K9d1DzU_yViQp1njpgFGY8jyKRqlZ-6w2VZSHLU2jAA2-tHv9XyIcGL05RrqtrPc9oxPQAOq0G-x0-yJc48gnBj2bJqVx-n-TbxTwlrGAZMMb1v6msyotp0RacAjOI5UMXdpJXk/s1600/6.jpg" /></a></div>
<p>
Now "sa" account is enabled and you can login to the SQL instance using the "sa" account after restarting the SQL Server.
</p>
<p>
Note: <b>SQL Server service needs to be restarted to make this change effective</b>.
</p>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com17tag:blogger.com,1999:blog-790956358810066267.post-33290935574897252942010-10-05T15:49:00.002+05:302015-04-22T01:27:01.538+05:30Install IIS in Windows 7 or Vista<p>
This tutorial discuss how you can enable (Internet Information Service) IIS in Windows 7 or Windows Vista. I have provided screen-shots so that you can easily understand.
</p>
<p>
First go to control panel and select Programs.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMt3ULwMIk8Pla89fY1inLsMUIusqZgoixM2nnZZvNuRbHHYiruHBf4vZFHAv2O0xpTAzrKs9n4moTrrRk_mzRc7RThcBRJuvHu_-KHPs6heesdCzSpjaZSsZoAMstlLJfDHn38ZdyMyyu/s1600/1.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMt3ULwMIk8Pla89fY1inLsMUIusqZgoixM2nnZZvNuRbHHYiruHBf4vZFHAv2O0xpTAzrKs9n4moTrrRk_mzRc7RThcBRJuvHu_-KHPs6heesdCzSpjaZSsZoAMstlLJfDHn38ZdyMyyu/s1600/1.gif" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhymW30uRuT_kUWCPP067uo2kzCZPsWEannY1m-xAiTnh1R0TnnMs630sdDnETuKlwzibjQTNRjS4rSkjCGX1f-8klQ7uEwf1dUMz5Z5wtC8ylglqHfXJiaXdiumGgTnd86q5jEAgzlaBoU/s1600/2.gi" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhymW30uRuT_kUWCPP067uo2kzCZPsWEannY1m-xAiTnh1R0TnnMs630sdDnETuKlwzibjQTNRjS4rSkjCGX1f-8klQ7uEwf1dUMz5Z5wtC8ylglqHfXJiaXdiumGgTnd86q5jEAgzlaBoU/s1600/2.gi" /></a></div>
<p>
Then select Internet Information Services. (If you plan to run web services you have to select ASP.NET under Application Development Features as following figure)
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiktVZJxJra0Pczp9eRrnMCJru9j4pKkqerM4_rBkNAZpr4UuRyyzh_lm1H6JxSeXWr-2vyHWLFerYrBTNtyI8tQ0YW-jBhA5qN_SWT0L89e4MsmLUU2y6Rw4wXm8pPvfmnjBi0spHd3T0D/s1600/3.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiktVZJxJra0Pczp9eRrnMCJru9j4pKkqerM4_rBkNAZpr4UuRyyzh_lm1H6JxSeXWr-2vyHWLFerYrBTNtyI8tQ0YW-jBhA5qN_SWT0L89e4MsmLUU2y6Rw4wXm8pPvfmnjBi0spHd3T0D/s1600/3.gif" /></a></div>
<p>
Now you have enabled IIS and try "<span style="color: #660000;">http://localhost</span>" in your browser and you will get following page.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi69FjBJzOys1L8wiUDLmK7-o7nZU4GSAjScPTTuMWnaTgGWJxj_KpPf2NhaQmR2n7-QPMfxYB1ZEcytXYvxRaiE8ty4njq-nw_NMV6FLBHgtdPuZKnSOaFBJk-IROI0JrXN97DrxJsv5Sy/s1600/4.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi69FjBJzOys1L8wiUDLmK7-o7nZU4GSAjScPTTuMWnaTgGWJxj_KpPf2NhaQmR2n7-QPMfxYB1ZEcytXYvxRaiE8ty4njq-nw_NMV6FLBHgtdPuZKnSOaFBJk-IROI0JrXN97DrxJsv5Sy/s1600/4.gif" /></a></div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com6tag:blogger.com,1999:blog-790956358810066267.post-46544558502592306692010-09-29T23:13:00.024+05:302015-04-22T01:49:24.050+05:30Create Simple Web Service with Visual Studio<p>
This tutorial explains how we can create simple Web Services using Visual Studio.
</p>
<h3>1. Create the Web Service</h3>
<p>
First create new project and select "<span style="color: #333333;">New ASP.NET Web Service Application</span>" and I'm giving the name "<span style="color: #660000;">MyFirstWebService</span>" to it, you can give any name to your project.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEix600GtejktUu9ntG3yxYYOuQ1-2Gy_aJMNaFsxejHWLPIiZVoWRP09hoe7N8TtxBYaRBPIyw9uYkbb9D3msU4RKvIVxadSRhwvuei9Mkzr8_B5gThG3hfFXPdKQ8exQjG2XTp9CgLwQEs/s1600/1.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEix600GtejktUu9ntG3yxYYOuQ1-2Gy_aJMNaFsxejHWLPIiZVoWRP09hoe7N8TtxBYaRBPIyw9uYkbb9D3msU4RKvIVxadSRhwvuei9Mkzr8_B5gThG3hfFXPdKQ8exQjG2XTp9CgLwQEs/s1600/1.gif" /></a></div>
<p>
Now you can see auto generated code that you can add methods to create your web service. You can see simple method "<span style="color: #660000;">HelloWorld</span>" and in this sample code I have removed it.
</p>
<p>
I'm going to add simple method called "<span style="color: #660000;">simpleMethod</span>" which takes a string as an input and add "Hello" to beginning of that string. Now the code will appear like bellow.
</p>
<pre class="brush: csharp;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace MyFirstWebService
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string simpleMethod(String srt)
{
return "Hello "+srt;
}
[WebMethod]
public int anotherSimpleMethod(int firstNum, int secondNum)
{
return firstNum + secondNum;
}
}
}
</pre>
<p>
Then you can run your code and you can see the resulting page as bellow.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDf5Vo0sUwB9jW4T4NBnGlVsj5PBCbfhk12l6UppMdHmds9LUkZC3g3Ortt9fZKgv6LBMiIcAW3ul4vXc78YkxJGpvGDc362xOmdY4pKA5Uaam-Yb0Pla9ICA4HHP6C8k-qXrCj7Ulfl6h/s1600/3.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDf5Vo0sUwB9jW4T4NBnGlVsj5PBCbfhk12l6UppMdHmds9LUkZC3g3Ortt9fZKgv6LBMiIcAW3ul4vXc78YkxJGpvGDc362xOmdY4pKA5Uaam-Yb0Pla9ICA4HHP6C8k-qXrCj7Ulfl6h/s1600/3.gif" /></a></div>
<h3>2. Create the Client Program</h3>
<p>
We have created our simple web service and we have to create small client program to use this web service. There you can open another instant of Visual Studio and create new "Console Application" project.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipsuKuFlIaSQ7MvOMM9LwBVFRwG8qaqdmkbCXryRjHNtjC0qi_h_24zRBuHEq0JIUQJQmXFvS9EJR6ijIibeOOf8tvwW0NfNeNSmwBCeMFoW6VJtUmHANSrRWzGsmRyDOvTP6FqUve7PlT/s1600/2.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipsuKuFlIaSQ7MvOMM9LwBVFRwG8qaqdmkbCXryRjHNtjC0qi_h_24zRBuHEq0JIUQJQmXFvS9EJR6ijIibeOOf8tvwW0NfNeNSmwBCeMFoW6VJtUmHANSrRWzGsmRyDOvTP6FqUve7PlT/s1600/2.gif" /></a></div>
<p>
Then you have to add Service Reference so that you can access your web service. Here are the screen-shots.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqWZGTsh02A8WzZoPHvLJAWOdA3d_F4li09uioI3uWmHddD95EBW44T_i5-qhQrw-Otzilc2TskO99XdDT9gHboWVp44aGT3oyv60MKSWZ38WYLlOiAOBJcW4UGXXuFy2lNQ_NQ1s4d8E1/s1600/4.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqWZGTsh02A8WzZoPHvLJAWOdA3d_F4li09uioI3uWmHddD95EBW44T_i5-qhQrw-Otzilc2TskO99XdDT9gHboWVp44aGT3oyv60MKSWZ38WYLlOiAOBJcW4UGXXuFy2lNQ_NQ1s4d8E1/s1600/4.gif" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgh4KD-_5RXFKfirYQmXO7rx8aEa1pT62HEd9y6DhAsoJ0Y7g32NZ_Qn29eJwxquK0ZJMuO9H983XxA6Y_WyD_DZI74dgZ3RKK-qZ8WduFF_7iJkG_ow4CIF7NfhiYVnFUfE2LXHTew38im/s1600/5.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgh4KD-_5RXFKfirYQmXO7rx8aEa1pT62HEd9y6DhAsoJ0Y7g32NZ_Qn29eJwxquK0ZJMuO9H983XxA6Y_WyD_DZI74dgZ3RKK-qZ8WduFF_7iJkG_ow4CIF7NfhiYVnFUfE2LXHTew38im/s1600/5.gif" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjo5pItgpRBJQ2zolbJ_xK3_5TINDGwASINgoJQVrE4IJlWe3s-C8LGmB9yDPWDKxM5l7peZQ0qTfD6-dQJ9J_cZBFwSbvU_niRIpwcfT5EN8HPcxhYZjtiZa9YLLMWTFoFegERrfp53HZL/s1600/6.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjo5pItgpRBJQ2zolbJ_xK3_5TINDGwASINgoJQVrE4IJlWe3s-C8LGmB9yDPWDKxM5l7peZQ0qTfD6-dQJ9J_cZBFwSbvU_niRIpwcfT5EN8HPcxhYZjtiZa9YLLMWTFoFegERrfp53HZL/s1600/6.gif" /></a></div>
<p>
Here you have to give the URL of the web service we created earlier. As I said before previously created web service application should be running on another instant of Visual Studio.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgRWuw_4jDm5bu2ioiZ883uqEZbvDXfTA0NZeAnR4k1RKVvY-11izbkQl84rwxUKrDSGIg4xC1pYHjY55xXdfz8RNwXWgRvgrk-_gYBhYMeLdRTc3dNygKNt3mv9PGqU9UpQC5qQLnggoR/s1600/7.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgRWuw_4jDm5bu2ioiZ883uqEZbvDXfTA0NZeAnR4k1RKVvY-11izbkQl84rwxUKrDSGIg4xC1pYHjY55xXdfz8RNwXWgRvgrk-_gYBhYMeLdRTc3dNygKNt3mv9PGqU9UpQC5qQLnggoR/s1600/7.gif" /></a></div>
<p>
Note that I have set the "<span style="color: #660000;">Web reference name</span>" as "<span style="color: #660000;">TestWeb</span>".
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBbJOwGdzF5i7R_E6vnFYqtSTTc5cRIXDfK0SZ8y-_3HXzIpltK8fT0dqRs0vuFf9nU16MBaPgJFBy5dimAj68n3FVcwMUy-u_J4S8MJKMkOO5e8eF3nakDizr2nXs7LHMYL7ej9f-GWD1/s1600/8.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBbJOwGdzF5i7R_E6vnFYqtSTTc5cRIXDfK0SZ8y-_3HXzIpltK8fT0dqRs0vuFf9nU16MBaPgJFBy5dimAj68n3FVcwMUy-u_J4S8MJKMkOO5e8eF3nakDizr2nXs7LHMYL7ej9f-GWD1/s1600/8.gif" /></a></div>
<p>
Now you can update your client program using following code. Note the line 5 "<span style="color: #660000; font-family: "Courier New",Courier,monospace;">using WebServiceTest.TestWeb;</span>".
</p>
<pre class="brush: csharp;highlight: [5]">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServiceTest.TestWeb;
namespace WebServiceTest
{
class Program
{
static void Main(string[] args)
{
Service1 webservice = new Service1();
string srt = webservice.simpleMethod("Saranga Rathnayake");
Console.WriteLine(srt);
Console.WriteLine(webservice .anotherSimpleMethod(4,3));
}
}
}
</pre>
<p>
Now you can run the client program to see the result.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpQUFMIonsyQ_acr-U7q6qVGiwLUXzAtMxQEAg8FMpMRSLnkG5Bcks_7yPU-9x6NUpcAt9zrK19-FvCMFm1lAcISXVwX_u-GmS3ttrJZuPt_dJAXheLgmSWDGD8GK4IGlStlC7ug32pHpz/s1600/9.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpQUFMIonsyQ_acr-U7q6qVGiwLUXzAtMxQEAg8FMpMRSLnkG5Bcks_7yPU-9x6NUpcAt9zrK19-FvCMFm1lAcISXVwX_u-GmS3ttrJZuPt_dJAXheLgmSWDGD8GK4IGlStlC7ug32pHpz/s1600/9.gif" /></a></div>
<h3>3. Publish Our Web Service in Internet Information Service (IIS)</h3>
<p>
Let's see how we can publish our web service in IIS. Otherwise you always need to run your web service application in separate VS instant. There first stop the web service application and go to the Solution Explore and Right Click on the project. Then select "Publish...".
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibVbhyzQ_hi4DOKD2Kl6a5nEOeDM92MsMC5mmvZJMGZ2fcAbSQg-rkVHOg_u60LgvR_Lwh2I6PLRnU9Q2T3glY4qn-mTmwCTg8mOCLXMcceuCahISXmOHWv4XEpND6-kFHXRrzmMVz6MAN/s1600/publish.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibVbhyzQ_hi4DOKD2Kl6a5nEOeDM92MsMC5mmvZJMGZ2fcAbSQg-rkVHOg_u60LgvR_Lwh2I6PLRnU9Q2T3glY4qn-mTmwCTg8mOCLXMcceuCahISXmOHWv4XEpND6-kFHXRrzmMVz6MAN/s1600/publish.jpg" /></a></div>
<p>
Then the following window will appear and there you can directly publish to the IIS by selecting "Web Deploy" as the publishing method. But here I'm going to use the "File System as the publishing method. There you have to provide the target location. I have created new folder called "MyApp" in my D drive and selected it.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6KeSSB1FsPjHPJgERAUOj1mfjtnFM9l1erJ4FagKlfQsYv3Um8jjWlMz0cE12zHrGUw3fa6PvSRk0sJH6I4Tijp4yIcct-PXUWCvW1mGa_7tcZM6X-L4CzseB4YoS1FHo3ozR6i8UvKaW/s1600/publish1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6KeSSB1FsPjHPJgERAUOj1mfjtnFM9l1erJ4FagKlfQsYv3Um8jjWlMz0cE12zHrGUw3fa6PvSRk0sJH6I4Tijp4yIcct-PXUWCvW1mGa_7tcZM6X-L4CzseB4YoS1FHo3ozR6i8UvKaW/s1600/publish1.jpg" /></a></div>
<p>
Now click "Publish" and check the "MyApp" folder. There you will be able to see Service1.asmx file, Web.config file and bin folder which contains the DLL file has been generated. Then copy this "MyApp" folder to "wwwroot" folder. You may find it in "C:\inetpub\wwwroot".
</p>
<p>
Now <a href="http://sarangasl.blogspot.com/2010/10/install-iis-in-windows-7-or-vista.html" target="_blank">enable IIS in your computer</a> and open IIS Manager. I'm going to add my service to Default Web Site. There Right Click on the "Default Web Site" and click "Add Application...".
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpttlRzjCVvkihOo7LYq_WOTFrVN1-2JnCpUU2i8v0ywfXAezEZRqnYhcSMF0dek7L8QHmP8qJjQyplV42RsfJSRME3EKXKnCNWKFF6txMvkddmz-a6rxtQ5MqScC2V3aw4VB1e-IBRakg/s1600/publish2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpttlRzjCVvkihOo7LYq_WOTFrVN1-2JnCpUU2i8v0ywfXAezEZRqnYhcSMF0dek7L8QHmP8qJjQyplV42RsfJSRME3EKXKnCNWKFF6txMvkddmz-a6rxtQ5MqScC2V3aw4VB1e-IBRakg/s1600/publish2.jpg" /></a></div>
<p>
There you will get following window. Now you can provide appropriate Alias (I have given testservice) and select the physical path of your application. There you can provide the path to the folder we copied previously as following figure and click Ok.
</p>
<p>
<span style="color: red;">
You have to make sure that the application pool identity has Read access to the physical path. So it is better if you copy your files to the "wwwroot" folder other than keep it in separate partition. Please check the following screen-shot</span>
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRMVzF29mms1irFe3gvOkk-9vgqYMb857s5xnY4BWqV9mxmXgYVGpYgbb9J4JNRoSX1FZlgL7GnFWiaU2j7moHcED_9EIN2yV53aSAnqNuN3x2p_h0oAeIokJOyNv6xTFPayV8x32DIgQ3/s1600/capture.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRMVzF29mms1irFe3gvOkk-9vgqYMb857s5xnY4BWqV9mxmXgYVGpYgbb9J4JNRoSX1FZlgL7GnFWiaU2j7moHcED_9EIN2yV53aSAnqNuN3x2p_h0oAeIokJOyNv6xTFPayV8x32DIgQ3/s1600/capture.jpg" /></a></div>
<p>
Now restart the IIS and goto http://localhost/testservice/Service1.asmx. You will be able to see the Web Service running.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEganSlOYBIZtpZn1-xGJXoPKE2UlLVCJJ28ZrSWim26lHeb5MRSk5YEixnmQPb-sb4t9x8dBogj2T0z-vgQ64XDzTAfS3JuEuIy2xYYH1S6TbreoETFrqzBZDkRgWCOfxvUeedEUg8sheJI/s1600/publish4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEganSlOYBIZtpZn1-xGJXoPKE2UlLVCJJ28ZrSWim26lHeb5MRSk5YEixnmQPb-sb4t9x8dBogj2T0z-vgQ64XDzTAfS3JuEuIy2xYYH1S6TbreoETFrqzBZDkRgWCOfxvUeedEUg8sheJI/s1600/publish4.jpg" /></a></div>
<p>
Now you have published your web service in IIS and you can update the Client Program by giving the new Web Reference URL using Properties Window.
</p>
<p>
If you are interested in creating web services in java, please follow my post <a href="http://blog.sencide.com/2011/06/create-web-service-using-apache-axis2.html" target="_blank">Create Web Service in Java Using Apache Axis2 and Eclipse</a>
</p>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com106tag:blogger.com,1999:blog-790956358810066267.post-36654717779217006842010-02-20T20:11:00.008+05:302015-04-22T02:09:39.068+05:30Form Validation and Suggestions using JQuery & PHP<p>
In this post I’m going to describe about a practical use of my previous article <a href="http://sarangasl.blogspot.com/2010/02/validation-and-suggestions-using-jquery.html" target="_blank">JQuery AJAX</a>. Here I’m going to discuss how to validate a form and how to add auto suggests to a form using JQuery and PHP.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgryqr6Djq3opYkIpFpwsbDDR99kk4MpoMmuIPvHBaEuqvRL8vDl_SRPz8kPZvfff_YTu-R3FTpQHiw4VS6-5vW3XdSb_2x_YcoyZSkwbnPSQK_NgWph8NiPGH_Vcj4ZwuFh56A1JbNaLCx/s1600/form.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgryqr6Djq3opYkIpFpwsbDDR99kk4MpoMmuIPvHBaEuqvRL8vDl_SRPz8kPZvfff_YTu-R3FTpQHiw4VS6-5vW3XdSb_2x_YcoyZSkwbnPSQK_NgWph8NiPGH_Vcj4ZwuFh56A1JbNaLCx/s1600/form.gif" /></a></div>
<p><a href="https://www.dropbox.com/s/lkvuzovpnz94ipt/jquery_form.zip?dl=0" target="_blank">Download Source</a> (Password : sara)
<p>
In the above I have given you the source code of the whole project which saves the collected data to a MySQL database. It is good if you have a basic knowledge about JavaScript and PHP to understand the codes. You can learn them very easily by referring <a href="http://www.w3schools.com/" target="_blank">w3schools</a>.
</p>
<p>
Since this I’m using PHP and MySQL in the above project, you have to run the code in a server like XAMP or WAMP. In this example I’m going to illustrate with WAMP. If you have the downloaded source code, you can see a folder called "jquery_form" and within that you can find folders css,js,scripts and images. Also you can see a file called index.php.
</p>
<p>
First thing you have to do is copy the "jquery_form" folder to your WAMP server’s "www" folder. Then open the "dbc.php" file inside the "scripts" folder and change its attributes. If you already haven’t a database called "userdata" and you haven’t given a password for root account, you don’t need to make any changes.
</p>
<pre class="brush: php"><?php
$host = "localhost";
$dbname = "userdata";
$username = "root";
$password = "";
?>
</pre>
<p>
Now do the following things when the WAMP server started. I’ll also mention the result of the given action here.
</p>
<p>
1. First run the "sql.php" file located in the "scripts" folder. The only thing you need to do is give the address to the file in the address bar. E.g. http://localhost/jquery_form/scripts/sql.php. This should be done when the first time you run the project.
</p>
<p>
I wrote this file to create a MySQL database with one table and needed fields to store the collected data. If you didn’t make changes to "dbc.php" file, there will be a databased called "userdata" and table called "details_user".
</p>
<p>
2. Then run the "index.php" file and fill the form correctly and submit. You will see your data in the database. If you try to give the same login name again, you will see the suggestions related to the name and birthday.
</p>
<p>
Let’s see the codes and check how we can add validation and suggestions. Here the JavaScript which do the above task is "validation.js" which is located in "js" folder. I’ll put the real line numbers in following piece of codes as they appear in actual file.
</p>
<pre class="brush: js;first-line: 8;"><title>JQuery Ajax Form | Saranga Rathnayake</title>
<!-- CSS -->
<link rel="stylesheet" href="css/styles.css" type="text/css"/>
<link type="text/css" href="css/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<!-- JavaScript -->
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<script src="js/validation.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$('#datepicker').datepicker({
dateFormat:'dd/mm/yy',
changeMonth: true,
changeYear: true,
yearRange: '-90:+0',
maxDate: '+0'
});
});
</script>
</head>
<div id="container">
<form id="logform" name="logform" class="logform" method="post" action="" >
<?php if(
</pre>
<p>
Above lines are from the "index.php" file, there I have linked necessary CSS and JavaScipt files. You can see a JavaScipt code which use to DatePicker.
</p>
<pre class="brush: js;first-line: 24;">//On blur
name.blur(validateNameAndSuggest);
iname.blur(validateIName);
address.blur(validateAddress);
email2.blur(validateEmail);
nic.blur(validateNIC);
phno.blur(validatePhNo);
email.blur(validateLoginEmailAndSuggest);
imgveri.blur(validateImg);
// on changed
bday.change(validateDOB);
//On key press
//name.keyup(validateName);
//iname.keyup(validateIName);
</pre>In the above lines I have called the validation functions in "blur" and "keyup" events. I have commented the "keyup" events, if you want them you can uncomment. Then I’ll show you how the validation works.<br />
<pre class="brush: js;first-line: 190;">function validateNIC(){
var a = $("#nic").val();
var filter = /^[0-9]{9}[v|V]$/;
if(filter.test(a)){
nic.removeClass("error");
nicInfo.text("");
nicInfo.removeClass("error");
return true;
}
//if it's NOT valid
else{
nic.addClass("error");
nicInfo.text("Type a valid NIC please, format: xxxxxxxxxV");
nicInfo.addClass("error");
return false;
}
}
</pre>
<p>
In the above code I have use Regular Expressions to make the validation more easier. If the Regular Expression dose not match with the input, then I have changed the CSS Class of the text box which change its color. And also I have added some information about the error. Then let’s see how suggestions work.
</p>
<pre class="brush: js;first-line: 314;">function suggestLogginName(){
var name = $("#fname").val();
var bday = $("#datepicker").val();
if(validateName() && validateDOB()){
$.post("scripts/isValiedName.php", { loginName:name, birthday:bday,suggestion:"OK" },
function(data){
emailInfo.html(data);
});
}
}
</pre>
<p>
The above function calls if the entered login name exist in the database. It will post the Full Name and Birthday to "isValiedName.php" file and that PHP file will return suggested names.
</p>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com6tag:blogger.com,1999:blog-790956358810066267.post-24103197812351762112010-01-26T21:22:00.005+05:302015-04-22T02:19:08.751+05:30AJAX Using JQuery Tutorial<p>
You may know that, JQuery is a JavaScript library that we can do smart things using small pease of code. This free and open source JavaScript Library has become the most famous JavaScript Library today because of its Lightweight, Cross-browser and CSS3 Compliant.
</p>
<p>
Using JQuery we can do thing like Document Navigating, DOM Element Selecting, Creating Animations, Event Handling and Ajax Development very easily. You can learn more about JQuery from <a href="http://jquery.com/" target="_blank">JQuery web site</a>.
</p>
<p>
<b>What this post about ?</b>
</p>
<p>
This is about a thing that we mostly want when we design web sites. I'm going to discuss how we can get that work done better and smarter using JQuery AJAX. Look at the following picture which describes the whole task, we are going to do.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHACZ6zVimbadNZO_nFSvlau6HjCq2gy90WH37ppawfUq_9M6PeWXLIefcySN9FfmZgeFuNFSc9xyk29lU7E0TOeUiBE5qR6iIP8DcQqihC6uYb3_RAIQI2tBjWWlWSCba3zvNGy5W_9k/s1600/layout.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHACZ6zVimbadNZO_nFSvlau6HjCq2gy90WH37ppawfUq_9M6PeWXLIefcySN9FfmZgeFuNFSc9xyk29lU7E0TOeUiBE5qR6iIP8DcQqihC6uYb3_RAIQI2tBjWWlWSCba3zvNGy5W_9k/s1600/layout.JPG" /></a></div>
<p>
Just imaging that the above picture is the structure of our web site, when we click on the links at "C", we want to load different contents to the main div in "B". If we use different web pages for all those links and load them when user clicks on a link, we are loading the content on "A" and "C" unnecessarily. That is why we should use AJAX to load the content.
</p>
<p>
First, download the following example code that I'm going to describe.
</p>
<p>
<a href="https://www.dropbox.com/s/rs24074h0wwovuu/jquery_ajax.zip?dl=0" target="_blank">Download Source Code</a>(Password : sara)
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-X_7itWggapwShz8HO41oSwkWasZGAaHUqTdiWC4Zp1sGLbg8ATAQ3j8XPsYB9QNANhM5Wi1LBF6k8gVq2eANHSRu23vIlMKyYSfDaNQuCWJgmAJ_Hgi3giBatpuSd0J1uFMGW7LjDnM/s1600/view.JP" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-X_7itWggapwShz8HO41oSwkWasZGAaHUqTdiWC4Zp1sGLbg8ATAQ3j8XPsYB9QNANhM5Wi1LBF6k8gVq2eANHSRu23vIlMKyYSfDaNQuCWJgmAJ_Hgi3giBatpuSd0J1uFMGW7LjDnM/s1600/view.JP" /></a></div>
<p>
Open the "index.html" file in your browser and check the result by clicking links "Home", "About" and "Contact Us" on "C". Ignore the problem with last link, I'll explane that later. Now open the "index.html" using a text editor. Then you will see a code like following in the <head> </head> section.
</p>
<pre class="brush: js;">
<script src="jquery-1.3.1.min.js" type="text/javascript" ></script>
</pre>
<p>
That is how you can link the JQuey to your web site, it's the same way you link a JavaScript file. Then check the following peace of code.
</p>
<pre class="brush: js;highlight: [3,5,9,23]">$(document).ready(function() {
//$("#main").load("home.html");
$("#link1").click(function(){
$("#main").load("home.html");
});
$("#link2").click(function(){
$.ajax({
url: "about.html",
cache: true,
beforeSend: function(){
$("#main").slideUp("fast");
},
success: function(html){
$("#main").slideDown("slow");
$("#main").html(html);
}
});
});
$("#link3").click(function(){
$.post("boo.php", { link_id:"link3" },
function(data){
$("#main").html(data);
});
});
});
</pre>
<p>
In JQuery we are writing events inside <a href="http://docs.jquery.com/Tutorials:Introducing_$%28document%29.ready%28%29" target="_blank">$(document).ready function</a>. In the above code I have given different IDs to links and call functions when they are clicked. There I have used three different ways to get the above task done.
</p>
<p>
First look at the 5<sup>th</sup> line, there I have load the "home.html" file inside the main div when click the "Home" link (id is "link1"). That is the easiest way to do a AJAX request. Then check the 9<sup>th</sup> line. There I have load the "about.html" file inside the main div when click the "About" link (id is "link2"). This method can be used to decorate our request <a href="http://docs.jquery.com/Effects" target="_blank">using effects</a>. As above code you can use beforeSend and success functions to get that work done.
</p>
<p>
If I uncomment the 3<sup>rd</sup> line, "home.html" page will load to main div in the page load. Finally check the 23<sup>rd</sup> line, this is different than above mentioned methods. Here I have get the data in a PHP file. I have call the "boo.php" file using POST method and display the result inside main div when "Contact Us" (id is "link3") clicked. Now I think you may noticed that AJAX is simple if you use JQuery.
</p>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com4tag:blogger.com,1999:blog-790956358810066267.post-50148310622759963652010-01-05T09:52:00.003+05:302015-04-22T02:25:53.076+05:30Embed a Picture to Body of Gmail Message<p>
In this post I’m going to show you how to embed a picture to body of the message in Gmail without attaching. To get this work done you have to enable Image Inserting in Google Labs. Let’s see how we can enable this.
</p>
<p>
First log in to your account and go to Settings as following picture;
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEdgggs4RZKo8_VPBBPjwieigX3C3oTVXjK4Q3CTdQygI11GkzkE6QM7erh4a3UP3YUF1T_lXqybeV0RO-_OqLLh-8fpeVDi1LMMIMeGR_eTV54ZFBPdJfmb2Thv_KKxfutmy9nZ8aNYCl/s1600/settings.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEdgggs4RZKo8_VPBBPjwieigX3C3oTVXjK4Q3CTdQygI11GkzkE6QM7erh4a3UP3YUF1T_lXqybeV0RO-_OqLLh-8fpeVDi1LMMIMeGR_eTV54ZFBPdJfmb2Thv_KKxfutmy9nZ8aNYCl/s1600/settings.JPG" /></a></div>
<p>
Then go to Labs section and there you can find Inserting Images. Enable the feature and save the settings. See the following screen shots to more clarifications.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEit0AX5g67mIGEO4HkyDGU2lEFMyhZtwfPw2X8lGEi3PW1ylrhci5OVwZLlxBxIFLl2KQfdTfQ3hZJT7iWimWq_U46FU9HHYG4GXdbcrYeK0FRjqu-RJQRhFVRFqwdSEVmpmoR3vqeKvpGr/s1600/labs.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEit0AX5g67mIGEO4HkyDGU2lEFMyhZtwfPw2X8lGEi3PW1ylrhci5OVwZLlxBxIFLl2KQfdTfQ3hZJT7iWimWq_U46FU9HHYG4GXdbcrYeK0FRjqu-RJQRhFVRFqwdSEVmpmoR3vqeKvpGr/s1600/labs.JPG" /></a></div>
<p>
If you have new interface you can directly go to Lab as follows.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhu7fVcGgHQXewSBnYmJ0YIQc-B4u44t2RnLrYvexf0xVMcwr1hR9VRtIr82IBCUxtf2rXZHkeuUPQnPYp3GVPPaiWavKPAnyxHNMqWg6NgXudZRLnkH49CNi7GaSseYV6A-yzzh9Ubzfo/s1600/Capture.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhu7fVcGgHQXewSBnYmJ0YIQc-B4u44t2RnLrYvexf0xVMcwr1hR9VRtIr82IBCUxtf2rXZHkeuUPQnPYp3GVPPaiWavKPAnyxHNMqWg6NgXudZRLnkH49CNi7GaSseYV6A-yzzh9Ubzfo/s1600/Capture.JPG" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRHco4RIU2bNKYc6rKRJ17r2G2UWl6CTsbmm6_g6OViTcuBlvWqAvizehdRguvkSjLa-WHYTP-h1goPV1MdQDe7vm_K59JmVB7DbFRhfKwFcaSc8agIwQV6v27KM1WjeyN8NLFwX7a-lYd/s1600/enable_insert_image.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRHco4RIU2bNKYc6rKRJ17r2G2UWl6CTsbmm6_g6OViTcuBlvWqAvizehdRguvkSjLa-WHYTP-h1goPV1MdQDe7vm_K59JmVB7DbFRhfKwFcaSc8agIwQV6v27KM1WjeyN8NLFwX7a-lYd/s1600/enable_insert_image.JPG" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcA2Atk7mfknEIY0E4AIlScJmPDpWfXZSHBIVRGFWM-iLPTbYk430H8_9RBisUPCllfEBRmRDvX-p5ezzfOVRPxqiGYMTJdYuu_ipH9eWfoozYJCDC0cZ6IEGoPRfLsfHGoulfzCUwz-MU/s1600/save.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcA2Atk7mfknEIY0E4AIlScJmPDpWfXZSHBIVRGFWM-iLPTbYk430H8_9RBisUPCllfEBRmRDvX-p5ezzfOVRPxqiGYMTJdYuu_ipH9eWfoozYJCDC0cZ6IEGoPRfLsfHGoulfzCUwz-MU/s1600/save.JPG" /></a></div>
<p>
Now you can see new icon in tool bar, using that we can embed images to body of the message as following picture.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBj2nO_FP6cB3rWK0An3oLq38UmuAbAhdk_NdVL7SU127yYktm5cdZ9xhrpr-tZ8y5s-3WPKOgbusTeVBLbcQ7Bnq20c67SBdtmoqmYHoihQqGtl-YEg9snXH0SXPte8VQpO3bl1zfhp23/s1600/new_mail.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBj2nO_FP6cB3rWK0An3oLq38UmuAbAhdk_NdVL7SU127yYktm5cdZ9xhrpr-tZ8y5s-3WPKOgbusTeVBLbcQ7Bnq20c67SBdtmoqmYHoihQqGtl-YEg9snXH0SXPte8VQpO3bl1zfhp23/s1600/new_mail.JPG" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ4wCXjXoKcxCSG9jSamT1r1isw91fsVv_XXd2MMUyDE2g7qibB3IC0pMjSdPkEqFPVOEK_ibYOr40gWRPqTeCz3RHnQhkd3xqUBFFME-_Xck15IVtd3sNyzyFofDDkScij8aMSSZ658iF/s1600/no_attach.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ4wCXjXoKcxCSG9jSamT1r1isw91fsVv_XXd2MMUyDE2g7qibB3IC0pMjSdPkEqFPVOEK_ibYOr40gWRPqTeCz3RHnQhkd3xqUBFFME-_Xck15IVtd3sNyzyFofDDkScij8aMSSZ658iF/s1600/no_attach.JPG" /></a></div>
<p>
This is a simple thing that most people dose not aware.
</p>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com8tag:blogger.com,1999:blog-790956358810066267.post-41983462346112010542009-12-28T17:52:00.006+05:302010-09-06T00:06:53.980+05:30ADD|Update|Delete Item SharePoint Web Services<div style="font-family: Verdana,sans-serif;"><br />
In one of my previous article I discussed how we can retrieve data in a SharePoint List Using its Web Services. In this post I’m going to discuss how we can update a SharePoint list using Web Services. Please refer my previous article on "<a href="http://sarangasl.blogspot.com/2009/12/sharepoint-list-web-service.html" target="_blank">SharePoint List Web Service GetListItems</a>" to learn how to add Web References to your project. Then you can use the following sample codes to Update SharePoint lists.<br />
<br />
<b>Updating Existing Items</b><br />
<br />
In the following code I have update the "Tasks" list, there I have update the title of two items which has the ID 7 and 10.<br />
<pre class="brush: csharp"> public void updateListItemsWS()
{
WS_Lists.Lists myservice = new WS_Lists.Lists();
myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
myservice.Url = "http://mermoss:5050/testsara/_vti_bin/Lists.asmx";
try
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ListVersion", "1");
string strBatch = "<Method ID='1' Cmd='Update'>" +
"<Field Name='ID'>7</Field>" +
"<Field Name='Title'>Sara1</Field></Method>" +
"<Method ID='2' Cmd='Update'><Field Name='ID' >10</Field>" +
"<Field Name='Title'>Sara2</Field></Method>";
elBatch.InnerXml = strBatch;
myservice.UpdateListItems("Tasks", elBatch);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
</pre><b>Delete Items</b><br />
<br />
To delete item, use following phrase in the above code, this will delete the item which has ID of 10.<br />
<br />
<div class="sourcecode">string strBatch = "<Method ID='1' Cmd='Delete'>" +<br />
"<Field Name='ID'>10</Field></Method>";<br />
</div><br />
<b>Add New Item</b><br />
<br />
To add item, use following phrase in the above code, <br />
<br />
<div class="sourcecode">string strBatch = "<Method ID='1' Cmd='New'>" +<br />
"<Field Name='ID'>New</Field>"+<br />
"<Field Name='Title'>TestTitle1</Field>"+<br />
"</Method>";<br />
</div><br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com6tag:blogger.com,1999:blog-790956358810066267.post-51052829738449704182009-12-17T10:01:00.004+05:302010-09-06T00:05:51.587+05:30SharePoint List Web Service GetListItems<div style="font-family: Verdana,sans-serif;"><br />
In this post, I’ going to discuss how we can retrieve data from a SharePoint List using its Web Services. First to know about SharePoint Web Services please refer <a href="http://msdn.microsoft.com/en-us/library/ms479390.aspx">this</a>. We can user <a href="http://msdn.microsoft.com/en-us/library/lists.aspx">List Web Service</a> which provides methods for working with SharePoint Lists, Content Types, List Items, and Files to read a List. Here we are going to use <a href="http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx">GetListItems</a> method.<br />
<br />
To use the above method we should know the GUIDs of the target list and view. Please follow my article on <a href="http://sarangasl.blogspot.com/2009/12/sharepoint-list-guids-how-to-find.html">SharePoint List GUIDs</a> to see how we can get them.<br />
<br />
This is how you can Add Web Reference; I’ll get created Web Site Project in Visual studio to illustrate. Actually it is simple, first click on Add Web Reference and give the URL to the Web Service. <br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkrs1GNsOVQIcMO2lNENOTl6qlAP22HH33sZcrF6YmOpJX7p8NDDyYq_cv32hONHjNGYdSDWygoB6QBUW0QphZRmpe9fDmWVWlbUknM9N4n6C54x_2vmsqcL8DhyQohsiAk-BLLB5h-T0Z/s1600-h/add_web_reference.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkrs1GNsOVQIcMO2lNENOTl6qlAP22HH33sZcrF6YmOpJX7p8NDDyYq_cv32hONHjNGYdSDWygoB6QBUW0QphZRmpe9fDmWVWlbUknM9N4n6C54x_2vmsqcL8DhyQohsiAk-BLLB5h-T0Z/s320/add_web_reference.JPG" /></a><br />
</div><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgD1DE8ytLMuvGmdUp_2jNj1Uy3VyxRuobNdxFUB1FTQVS59L82klFfPxjP0kXPFSA9HexmwcYHtqwOPdXma4MHyYI5arus6QfHeyCdsAxCoAemlZpKfbjHR-ggFETHWYgCkrYXe-q4L4fN/s1600-h/add_web_reference1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgD1DE8ytLMuvGmdUp_2jNj1Uy3VyxRuobNdxFUB1FTQVS59L82klFfPxjP0kXPFSA9HexmwcYHtqwOPdXma4MHyYI5arus6QfHeyCdsAxCoAemlZpKfbjHR-ggFETHWYgCkrYXe-q4L4fN/s320/add_web_reference1.JPG" /></a><br />
</div><br />
This is a sample code to read SharePoint list,<br />
<pre class="brush: csharp"> public void getListData()
{
WS_Lists.Lists myservice = new WS_Lists.Lists();
myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
myservice.Url = "http://merdev-moss:5050/testsara/_vti_bin/Lists.asmx";
try
{
/* Assign values to pass the GetListItems method*/
string listName = "{5C65CB1A-2E1B-488A-AC07-B115CD0FC647}";
string viewName = "{75E689B4-5773-43CB-8324-58E42E1EB885}";
string rowLimit = "100";
// Instantiate an XmlDocument object
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
/*Use CAML query*/
query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" +
"<Value Type=\"Counter\">0</Value></Gt></Where>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
queryOptions.InnerXml = "";
System.Xml.XmlNode nodes = myservice.GetListItems(listName, viewName, query, viewFields, rowLimit, null, null);
foreach (System.Xml.XmlNode node in nodes)
{
if (node.Name == "rs:data")
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Name == "z:row")
{
Response.Write(node.ChildNodes[i].Attributes["ows_Title"].Value + "</br>");
}
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
</pre>In the above code I have use CAML queries to get the result. <span style="color: red;">It will return all the result because I’m searching for Items which has ID greater than 0</span>. It is always true. If you want to know how to write CAML quires easily please refer my article on <a href="http://sarangasl.blogspot.com/2009/10/caml-spquery-in-sharepoint.html">CAML & SPQuery in SharePoint</a>.<br />
<br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com31tag:blogger.com,1999:blog-790956358810066267.post-17854991698348265912009-12-16T12:51:00.004+05:302009-12-16T13:48:06.561+05:30SharePoint List GUIDs, How to Find?<div style="font-family: Verdana,sans-serif;"><br />
In this post I’m going to discuss about an easiest way to find <b>GUID of a SharePoint list and a View</b>. If you are using the MOSS 2007 there is straight forward way to get the List GUID, these are the steps.<br />
<br />
1. Access the list via your browser.<br />
<br />
2. Go to Settings --> List Settings and right-click on the "Information management policy settings" links and choose Copy Shortcut.<br />
<br />
3. Paste the URL and you can see the GUID surrounded with "{" and "}".<br />
<br />
If you are using WSS 3.0 follow these steps, you can use this method to find the View GUID in MOSS 2007<br />
<br />
1. Access the list via your browser and select the View you want to find the GUID.<br />
<br />
2. Select "Modify this View" and copy the URL.<br />
<br />
3 You will get a URL like this,<br />
<br />
<div class="sourcecode" style="font-family: "Courier New",Courier,monospace;">http://mysite:5050/testsara/_layouts/ViewEdit.aspx?List=%7B5C65CB1A%2D2E1B%2D488A%2DAC07%2DB115CD0FC647%7D&View=%7B75E689B4%2D5773%2D43CB%2D8324%2D58E42E1EB885%7D&Source=http%253A%252F%252Fmysite%253A5050%252Ftestsara%252FLists%252FTasks%252FAllItems%252Easpx<br />
</div><br />
There you can find,<br />
<br />
<div class="sourcecode" style="font-family: "Courier New",Courier,monospace;">List=%7B5C65CB1A%2D2E1B%2D488A%2DAC07%2DB115CD0FC647%7D<br />
View=%7B75E689B4%2D5773%2D43CB%2D8324%2D58E42E1EB885%7D<br />
</div><br />
<div class="sourcecode" style="font-family: "Courier New",Courier,monospace;">Replace "%7B" with "{"<br />
Replace all "%2D" with "-"<br />
Replace "%7D" with "}"<br />
</div><br />
You are now left with the GUIDs, in the above example;<br />
<br />
<div class="sourcecode" style="font-family: "Courier New",Courier,monospace;">List ID is {5C65CB1A-2E1B-488A-AC07-B115CD0FC647}<br />
View ID is {75E689B4-5773-43CB-8324-58E42E1EB885}<br />
</div><br />
You can use <a href="http://spstipsutilitypack.codeplex.com/">SharePoint Tips Utility Pack</a> if you need to get the <b>GUID of List columns</b>. There in menu bar, select "List Management --> Change Field Settings" and load your site.<br />
<br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com2tag:blogger.com,1999:blog-790956358810066267.post-36366627428421472812009-12-10T11:31:00.012+05:302011-11-28T21:50:24.155+05:30Add User To SharePoint Group Programmatically<div style="font-family: Verdana, sans-serif;"><br />
Let’s see how we can programmatically add a user to existing SharePoint user group . You can use the following method which has two string parameters, one for group name and other for user login name for get the above task done. <br />
<pre class="brush: csharp">public void addUserToGroup(string groupName, string userLoginName)
{
string strUrl = "http://mysite:5050/";
using (SPSite site = new SPSite(strUrl))
{
using (SPWeb web = site.OpenWeb())
{
try
{
web.AllowUnsafeUpdates = true;
SPUser spUser = web.EnsureUser[userLoginName];
if (spUser != null)
{
SPGroup spGroup = web.Groups[groupName];
if (spGroup != null)
spGroup.AddUser(spUser);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
web.AllowUnsafeUpdates = false;
}
}
}
}
</pre>Here you should provide the login name in correct format (DomainName\UserName). For example you can call this method like follow,<br />
<br />
<span style="font-family: 'Courier New', Courier, monospace;">addUserToGroup("Test Group", "SA\\saranga.rathnayaka");</span><br />
<br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com2tag:blogger.com,1999:blog-790956358810066267.post-47006439494956937322009-12-09T15:53:00.010+05:302011-11-01T22:08:33.901+05:30Add Item To List With LookUp Column Programmatically<div style="font-family: Verdana, sans-serif;"><br />
In my previous posts on "<a href="http://sarangasl.blogspot.com/2009/09/sharepoint-list-c-part-1.html">Working With SharePoint List</a>" I have discussed how we can Read, Add and Update SharePoint List. If you have to add new Item to a list which has lookup column, you have to follow a different procedure.<br />
<br />
If you look at the following figure, you can easily understand what I'm going to do. I'm going to add a new record to <span style="color: blue;">List_B</span>, which has a lookup column from <span style="color: blue;">List_A's</span> Title Field.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNPOBJLPW4InLnMqguGtTiw4YVVl2rVmz8zNdOtQOGAsVT-MjolwTdyzUGcvrqKEzDSBfzNFHLUE6I9cSzFVslmFCJOnTgeBsqxCpZUm4SjpnpKwA8C2KpmfcFjnF9OkDeYGxU_Eom8M3Y/s1600-h/add_lookup_item.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNPOBJLPW4InLnMqguGtTiw4YVVl2rVmz8zNdOtQOGAsVT-MjolwTdyzUGcvrqKEzDSBfzNFHLUE6I9cSzFVslmFCJOnTgeBsqxCpZUm4SjpnpKwA8C2KpmfcFjnF9OkDeYGxU_Eom8M3Y/s320/add_lookup_item.JPG" /></a><br />
</div><br />
This is a sample code to get the above task done,<br />
<pre class="brush: csharp">
public void addToLookUp()
{
/* open the web */
SPSite site = new SPSite("http://merdev-moss:5050/testsara");
SPWeb web = site.OpenWeb();
string targetList = "List_B";
string sourceList = "List_A";
string sourceListField = "Title";
/* we are going to add new row to List_B,
* It (List_B) has a lookup column called "A_Column",
* It (A_Column) get data from Title field of List_A.
* (List_A has value called "test2" in Title field)
*/
SPListItemCollection listItems = web.Lists[targetList].Items;
SPListItem item = listItems.Add();
item["Title"] = "New Title";
int ID = get_ID(web, sourceList, sourceListField, "test2");
if (ID != 0)
item["A_Column"] = ID;
item.Update();
}
public static int get_ID(SPWeb web, string list, string field, string itemname)
{
int id = 0;
SPList sharedDocumentList = web.ServerRelativeUrl.Equals("/") ? web.GetList(web.ServerRelativeUrl + list) : web.GetList(web.ServerRelativeUrl + "/" + list);
SPListItemCollection listItems = sharedDocumentList.Items;
foreach (SPListItem item in listItems)
{
if (item[field].ToString() == itemname.ToString())
id = item.ID;
break;
}
return (id);
}
</pre>
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com5tag:blogger.com,1999:blog-790956358810066267.post-36327355312283337692009-12-08T17:50:00.007+05:302010-09-06T00:04:26.717+05:30Create SharePoint List View Programmatically<div style="font-family: Verdana, sans-serif;"><br />
In this post I’m going to describe how you can programmatically add a view to SharePoint list or Document Library. You can get this work done by using <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spviewcollection.add.aspx">SPViewCollection.Add Method</a> (Microsoft.SharePoint), here is a sample code,<br />
<pre class="brush: csharp"> public void createListView()
{
SPSite site = new SPSite("http://merdev-moss:5050/testsara");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Tasks"];
SPViewCollection allviews = list.Views;
string viewName = "Test View";
System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection();
viewFields.Add("Title");
viewFields.Add("Priority");
string myquery = "<Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where>";
allviews.Add(viewName, viewFields, myquery, 100, true, false);
}
</pre>Above code creates a view in the collection with the specified name "Test View" and added view fields. Query string will return all items, because always ID is greater than 0. Row limit is 100 and the view displays items page by page. Last Boolean mean it is not the default view.<br />
<br />
Please refer my article on <a href="http://sarangasl.blogspot.com/2009/10/caml-spquery-in-sharepoint.html">CAML & SPQuery in SharePoint</a> to find out easy way to write CAML quires used in above code.<br />
<h4>Add View Using Existing View</h4>You also can create new List View with the help of existing one, this sample code will create new View which has another field using above created View.<br />
<pre class="brush: csharp"> public void _createListView()
{
SPSite site = new SPSite("http://merdev-moss:5050/testsara");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Tasks"];
SPViewCollection allviews = list.Views;
string viewName = "New Test View";
System.Collections.Specialized.StringCollection viewFields = list.Views["Test View"].ViewFields.ToStringCollection();
viewFields.Add("Status");
string myquery = "<Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where>";
allviews.Add(viewName, viewFields, myquery, 100, true, false);
}
</pre></div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com0tag:blogger.com,1999:blog-790956358810066267.post-27427928720595243152009-12-08T12:33:00.006+05:302010-09-06T00:03:40.732+05:30Add Permission Level To SharePoint User Group<div style="font-family: Verdana,sans-serif;"><br />
In this post I'm going to discuss how you can add a permission level (Read, Contribute, Full Control, etc) to a SharePoint user group. Here is a sample code,<br />
<pre class="brush: csharp"> public void addPermissionToGroup()
{
SPSite site = new SPSite("http://mysite:5050/");
SPWeb spWeb = site.OpenWeb();
string permissionName = "Read";
string groupName = "Project Manager";
try
{
spWeb.AllowUnsafeUpdates = true;
SPRoleAssignment roleAssignment = new SPRoleAssignment(spWeb.SiteGroups[groupName]);
roleAssignment.RoleDefinitionBindings.Add(spWeb.RoleDefinitions[permissionName]);
if (!spWeb.HasUniqueRoleAssignments)
spWeb.BreakRoleInheritance(false);
spWeb.RoleAssignments.Add(roleAssignment);
spWeb.Update();
}
catch (Exception _exception)
{
throw _exception;
}
finally
{
spWeb.AllowUnsafeUpdates = false;
}
}
</pre>You can view RoleDefinitions defined for your site by visiting to <span style="color: red; font-family: "Courier New",Courier,monospace;">Site Settings --> Advanced Permissions</span>.<br />
<br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com2tag:blogger.com,1999:blog-790956358810066267.post-89645716718036019082009-12-02T16:35:00.015+05:302010-09-06T00:02:49.746+05:30Add Menu Bar To Blogger Blog<div style="font-family: Verdana,sans-serif;"><br />
I was able to add a nice multilevel dropdown menu to my blog. In this post I’m going to discuss how you also can add it to your blogger blog. Here I’m using pure CSS multilevel menu generated by <a href="http://purecssmenu.com/">Free CSS Drop Down Menu Generator</a>.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbxBcdy7T1sQ_p4pYHFxW5spjhHbCERph7KKuaDe0LvzDcRRch8K4H2QxA309ap8XYi0Er0ZBpehYJKRUS0s_5aCVQk5vWGvHfgMtlECTDDlh457D1vnr6yaSqK1uBn-oTtn22MGZqiq2N/s1600-h/menu_bar_blogger.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbxBcdy7T1sQ_p4pYHFxW5spjhHbCERph7KKuaDe0LvzDcRRch8K4H2QxA309ap8XYi0Er0ZBpehYJKRUS0s_5aCVQk5vWGvHfgMtlECTDDlh457D1vnr6yaSqK1uBn-oTtn22MGZqiq2N/s320/menu_bar_blogger.JPG" /></a><br />
</div><br />
You can’t use the code directly in blogger without some modifications; I’ll describe how you can edit the code to use it in blogger to get a menu bar like above picture (Figure 1).<br />
<h4 style="font-family: Verdana,sans-serif;">Create the Menu</h4>First get the following code and save it as .html file. Then edit the styles and links as you want. If you like this style, you only need add links for menus and sub menus.<br />
<br />
<span style="color: red;">Note :</span> If you use the above mentioned Menu Generator in the HTML code you must replace "<span style="color: red;"><![if gt IE 6]></a><![endif]></span>" using "<span style="color: red;"><!--[if gt IE 6]><!--></a><!--<![endif]--></span>", otherwise it will not work in the Blogger.<br />
<br />
<a aiotitle="click to expand" href="javascript:togglecomments('yourDivId')">View Code</a><br />
<br />
<div class="commenthidden" id="yourDivId"><pre class="brush: html"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
ul.cssMenu ul{display:none}
ul.cssMenu li:hover>ul{display:block}
ul.cssMenu ul{position: absolute;left:-1px;top:98%;}
ul.cssMenu ul ul{position: absolute;left:98%;top:-2px;}
ul.cssMenu,ul.cssMenu ul{
margin:0px;
list-style:none;
padding:0px 2px 2px 0px;
background-color:#d8d8d8;}
ul.cssMenu table {border-collapse:collapse}ul.cssMenu {
display:block;
zoom:1;
float: left;}
ul.cssMenu ul{
border-left:2px solid #444444;
border-bottom:1px solid #444444;
border-right:1px solid #444444;
border-top:0px;
z-index:9999;}
ul.cssMenu li{
display:block;
margin:2px 0px 0px 2px;
font-size:0px;}
ul.cssMenu a:active, ul.cssMenu a:focus {outline-style:none;}
ul.cssMenu a, ul.cssMenu li.dis a:hover, ul.cssMenu li.sep a:hover {
display:block;
vertical-align:middle;
background-color:#d8d8d8;
text-align:left;
text-decoration:none;
padding:4px;
_padding-left:0;
font:normal 11px Verdana;
color: #444444;
text-decoration:none;
cursor:default;}
ul.cssMenu span{
overflow:hidden;
font-weight:bold;
background-position:right center;
background-repeat: no-repeat;
padding-right:11px;}
ul.cssMenu li { float:left;}
ul.cssMenu ul li {float:none;}
ul.cssMenu ul a {
text-align:left;
white-space:nowrap;}
ul.cssMenu li.sep{
text-align:center;
padding:0px;
line-height:0;
height:100%;}
ul.cssMenu li.sep span{
float:none;
padding-right:0;
width:5;
height:16;
display:inline-block;
background-color:#AAAAAA;
background-image:none;}
ul.cssMenu ul li.sep span{width:80%;height:3;}
ul.cssMenu li:hover{position:relative;}
ul.cssMenu li:hover>a{
background-color:#eeeeee;
font:normal 11px Verdana;
color: #000000;
text-decoration:none;}
ul.cssMenu li a:hover{
position:relative;
background-color:#eeeeee;
font:normal 11px Verdana;
color: #000000;
text-decoration:none;}
ul.cssMenu li.dis a {color: #AAAAAA !important;}
ul.cssMenu img {border: none;float:left;_float:none;margin-right:4px;width:16px;height:16px;}
ul.cssMenu a:hover ul,ul.cssMenu a:hover a:hover ul,ul.cssMenu a:hover a:hover a:hover ul{display:block}
ul.cssMenu a:hover ul ul,ul.cssMenu a:hover a:hover ul ul{display:none}
</style>
</head>
<body>
<ul class="cssMenu">
<li><a href="#" title="test title"><span>SEO</span><!--[if gt IE 6]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#" title="test title">Your Site in Google</a></li>
<li><a href="#" title="test title">Selecting Good Title</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]--></li>
<li><a href="#" title="test title"><span>Blogger Tips</span><!--[if gt IE 6]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><![endif]-->
<ul class="cssMenum">
<li><a href="#" title="test title">Expandable Blogger Posts</a></li>
<li><a href="#" title="test title">Blogger Title Tip</a></li>
<li><a href="#" title="test title">Adding META Tags</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]--></li>
<li><a href="#" title="test title"><span>SharePoint</span><!--[if gt IE 6]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><![endif]-->
<ul class="cssMenum">
<li><a href="#" title="test title">What is SharePoint</a></li>
<li><a href="#" title="test title"><span>Create Custom Features</span><!--[if gt IE 6]><!--></a><!--<![endif]--><!--[if lte IE 6]><table><tr><td><![endif]-->
<ul class="cssMenum">
<li><a href="#" title="test title">Removing Features</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]--></li>
<li><a href="#" title="test title">CAML in SharePoint</a></li>
<li><a href="#" title="test title">User Groups</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]--></li>
</ul>
</body>
</html>
</pre></div><h4 style="font-family: Verdana,sans-serif;">Add Menu to Blog</h4>1. Now you have to edit the HTML in your blogger template. To do that first go to "Edit HTML" (Figure 2). <span style="color: red;">(Before editing your template, download the full template)</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHZpy67M1MFBHmWu-tvMvmJ8W3CsIpZ1p_BkTunQE3GQOM09s05Ry4dpqwCwU-vJv9YdTgNVAYVQkw98IKk941x7ltreXep6myiJVWQm9b6hEqFn04wvcpwIuim5ID13P5VjAWb15a6kbE/s1600-h/edit_template.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHZpy67M1MFBHmWu-tvMvmJ8W3CsIpZ1p_BkTunQE3GQOM09s05Ry4dpqwCwU-vJv9YdTgNVAYVQkw98IKk941x7ltreXep6myiJVWQm9b6hEqFn04wvcpwIuim5ID13P5VjAWb15a6kbE/s320/edit_template.JPG" /></a><br />
</div><br />
Find "<span style="color: red;">b:section class='header'</span>" and set the value of "<span style="color: red;">maxwidgets</span>" to 3, if it is less than it (Figure 2). Set "<span style="color: red;">showaddelement</span>" to "<span style="color: red;">true</span>",<br />
<br />
2. Then Find the <span style="color: red;"></head></span> tag in your template and paste the styles just before that tag. Make sure you should <span style="color: blue;">include </span><span style="color: red;"><style type="text/css"></span> and <span style="color: red;"></style></span> tags.<br />
<br />
3. Now you can add new "HTML/JavaScript" Gadget to your blogger header and paste the code <span style="color: blue;">between </span><span style="color: red;"><body></span> and <span style="color: red;"></body></span> from the .html file you created using my code given in the top of this post (Figure 3). <span style="color: red;">There do not give a Title for the Gadget</span>.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7pHBOYIeOsvPgzgY4Gfc-30W9CWkB3niU6qLn4kfmoaqaSbBeqPw-1hloUbaDnqqZBkQ9QtZpWmRRJKnqqXxycVYmrEQ8OQ-Uu8YaWpzHpSpfBTAs1JuWCdEyhTMj2bc0lxgz-Iv3yveD/s1600-h/add_wedgit.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7pHBOYIeOsvPgzgY4Gfc-30W9CWkB3niU6qLn4kfmoaqaSbBeqPw-1hloUbaDnqqZBkQ9QtZpWmRRJKnqqXxycVYmrEQ8OQ-Uu8YaWpzHpSpfBTAs1JuWCdEyhTMj2bc0lxgz-Iv3yveD/s320/add_wedgit.JPG" /></a><br />
</div><br />
Now every thing is ok and view your blog to see the new Gadget.<br />
<br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com14tag:blogger.com,1999:blog-790956358810066267.post-77666064242746001372009-11-26T13:53:00.014+05:302010-09-05T23:11:43.217+05:30Add Lookup Column to SharePoint List Programmatically<div style="font-family: Verdana,sans-serif;"><br />
In one of my previous article I discussed <a href="http://sarangasl.blogspot.com/2009/11/create-sharepoint-list-programmatically.html">how to create a SharePoint list programmatically in C#</a>. There I didn’t discuss how we can add lookup field for our newly created list. Let’s see how, this is a sample code.<br />
<pre class="brush: csharp"> public void createList()
{
// choose your site
SPSite site = new SPSite("http://merdev-moss:5050");
SPWeb web = site.OpenWeb();
SPListCollection lists = web.Lists;
// create new Generic list called "My List"
lists.Add("My List", "My list Description", SPListTemplateType.GenericList);
SPList newList = web.Lists["My List"];
// create Text type new column called "My Column"
newList.Fields.Add("My Column", SPFieldType.Text, true);
/*create lookup type new column called "Lookup Column"
* Here I am going to get the information from the "Title"
* column of a list called "User Roles"
*/
SPList targetList = web.Lists["User Roles"];
newList.Fields.AddLookup("Lookup Column", targetList.ID, false);
SPFieldLookup lkp = (SPFieldLookup)newList.Fields["Lookup Column"];
lkp.LookupField = targetList.Fields["Title"].InternalName;
lkp.Update();
// make new column visible in default view
SPView view = newList.DefaultView;
view.ViewFields.Add("My Column");
view.ViewFields.Add("Lookup Column");
view.Update();
}
</pre>In the above code I have created Generic list and lookup column. You can use whatever list type and field type according to your requirement. To learn more about SharePoint lists, follow "<a href="http://sarangasl.blogspot.com/2009/09/sharepoint-list-c-part-1.html">SharePoint List C# Part 1</a>".<br />
<br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com8tag:blogger.com,1999:blog-790956358810066267.post-17375330387025475712009-11-23T14:39:00.011+05:302010-09-14T22:48:58.133+05:30Remove Event Receiver in Sharepoint List<div style="font-family: Verdana,sans-serif;"><br />
In this post I’m going to describe how you can remove an Event Receivers from SharePoint List programmatically. This will be very useful if you have deployed and active custom features, because if you <a href="http://sarangasl.blogspot.com/2009/09/in-this-article-im-going-to-describe.html">create a custom feature</a> for a custom list, that feature will be added to all the custom lists. Then if you want to create list dynamically and don’t want to add those features you can use this code to remove unwanted events.<br />
<pre class="brush: csharp">public void removeEvents()
{
// choose your site
string strUrl = "http://mysite:5050/";
using (SPSite site = new SPSite(strUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPListCollection lists = web.Lists;
SPList list = web.Lists["My List"];
SPEventReceiverDefinitionCollection erdc = list.EventReceivers;
List <SPEventReceiverDefinition> eventsToDelete = new List <SPEventReceiverDefinition>();
foreach (SPEventReceiverDefinition erd in erdc)
{
if (erd != null)
{
try
{
eventsToDelete.Add(erd);
}
catch (Exception e)
{
Console.Write(e.ToString());
}
}
}
foreach (SPEventReceiverDefinition er in eventsToDelete)
{
//if(er.Type == SPEventReceiverType.ItemAdded)
er.Delete();
}
}
}
}
</pre>Before delete the Event Receiver, if you want you can check the type and delete. In the above code I have commented that part. So if you use the above code as it is, it will remove all the Event Receivers.<br />
<br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com2tag:blogger.com,1999:blog-790956358810066267.post-6509680067107218452009-11-13T18:32:00.023+05:302009-12-11T13:39:23.787+05:30SharePoint charting (Chart inside web part)<div style="font-family: Verdana,sans-serif;"><br />
In my article on "<a href="http://sarangasl.blogspot.com/2009/09/custom-sharepoint-webparts.html">SharePoint Web Part Development Tutorial</a>" I discussed how we can develop and deploy custom SharePoint web part with visual designing. Now let’s see how we can add a chart for that web part. Finally you will be getting a result like following picture.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZuFpfV0-GM0xPPSTvdlCs7QPisUTnGrwTEHRU-0LOHf4D1n5tr23koLKGpQqllLCq7SJNxAnRSAW42VCCQXq1dApEJ8NbNjUSTnSUFDix5C7fUI_8WlRDAhfPvFRg-7XJXjdgDsPP2aBB/s1600-h/chartwebpart.GIF" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZuFpfV0-GM0xPPSTvdlCs7QPisUTnGrwTEHRU-0LOHf4D1n5tr23koLKGpQqllLCq7SJNxAnRSAW42VCCQXq1dApEJ8NbNjUSTnSUFDix5C7fUI_8WlRDAhfPvFRg-7XJXjdgDsPP2aBB/s320/chartwebpart.GIF" /></a><br />
</div><br />
To create the above chart I’m getting data from a SharePoint list as follow.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7QoE51BFm6bIcIoHww2V0ckaCA_a4wKSn0jiVnk2DRwyEhee6U0eKBw4yJa-bbWunvFbZchAMgaWjS7YVcAcTOcLcZ2WIDQkL67TWnQj7S96Jl4aw2XtNpB3DEXvPmU86WuszaSFdrmLD/s1600-h/chart_web_part.GIF" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7QoE51BFm6bIcIoHww2V0ckaCA_a4wKSn0jiVnk2DRwyEhee6U0eKBw4yJa-bbWunvFbZchAMgaWjS7YVcAcTOcLcZ2WIDQkL67TWnQj7S96Jl4aw2XtNpB3DEXvPmU86WuszaSFdrmLD/s320/chart_web_part.GIF" /></a><br />
</div><br />
You can see I’m showing the chart inside my custom web part called "My first custom Web Part". There I used the code from "<a href="http://www.amrein.com/apps/page.asp?Q=5749">Free Google Chart SharePoint Web Part</a>" and customized it as I want. These are the steps to add nice chart to your web part for free.<br />
<br />
First <a href="http://sarangasl.blogspot.com/2009/09/custom-sharepoint-webparts.html">Create your custom web part</a>, don’t deploy at this stage.<br />
<br />
Then add a Class called “Chart.cs” and replace the code using following code.<br />
<br />
<a aiotitle="click to expand" href="javascript:togglecomments('chartcode')"><b>View Code</b></a><br />
<br />
<div class="commenthidden" id="chartcode" style="font-family: 'Courier New', Courier, monospace;"><div class ="sourcecode">using System;<br />
using System.Data;<br />
using System.Configuration;<br />
using System.Linq;<br />
using System.Web;<br />
using System.Web.Security;<br />
using System.Web.UI;<br />
using System.Web.UI.HtmlControls;<br />
using System.Web.UI.WebControls;<br />
using System.Web.UI.WebControls.WebParts;<br />
using System.Xml.Linq;<br />
using Microsoft.SharePoint;<br />
<br />
namespace ProjectView{<br />
public class Chart{<br />
<br />
protected string _BGColor = "FFFFFF";<br />
protected string _ChartColor = "0099ff,ff0000";<br />
protected int _ChartHeight = 400;<br />
protected LegendPositions _ChartLegend;<br />
protected string _ChartTitle = "Burn Down Chart";<br />
protected ChartTypes _ChartType;<br />
protected int _ChartWidth = 720;<br />
protected string _DataField = "Planned Effort,Actual Effort";<br />
protected string _LabelField = "Title";<br />
protected string _ListName = "";<br />
protected string _SiteName = "http://merdev-moss:5050/";<br />
protected string _ViewName = "";<br />
<br />
public enum ChartTypes{<br />
Line,<br />
VerticalBar,<br />
Pie,<br />
Pie3D,<br />
HorizontalBar<br />
}<br />
public enum LegendPositions{<br />
horizontal_top, <br />
vertical_left,<br />
horizontal_bottom,<br />
vertical_top,<br />
vertical_bottom,<br />
vertical_right<br />
}<br />
public string createChartString(string listName){<br />
_ListName = listName;<br />
<br />
string str = "";<br />
string str2 = "";<br />
<br />
SPSite site = new SPSite(this._SiteName);<br />
SPWeb web = site.OpenWeb();<br />
try{<br />
if (!string.IsNullOrEmpty(this._ListName)){<br />
SPList list = null;<br />
SPListItemCollection items = null;<br />
try{<br />
string[] strArray3;<br />
int num5;<br />
list = web.Lists[this._ListName];<br />
if (!string.IsNullOrEmpty(this._ViewName)){<br />
SPView view = list.Views[this._ViewName];<br />
items = list.GetItems(view);<br />
}<br />
else{<br />
items = list.Items;<br />
}<br />
string str3 = "";<br />
float num2 = 0f;<br />
int num3 = 0;<br />
bool flag = true;<br />
char[] separator = new char[] { ',', ';' };<br />
string[] strArray = this._DataField.Split(separator);<br />
string[] strArray2 = new string[strArray.Length];<br />
foreach (string str4 in strArray){<br />
str2 = str2 + "<br>Series=" + str4;<br />
if (!list.Fields.ContainsField(str4)){<br />
str = str + "<p>Data List column not specified or not found:" + str4 + "</p>";<br />
flag = false;<br />
}<br />
}<br />
if (!list.Fields.ContainsField(this._LabelField)){<br />
str = str + "<p>Label List column not specified or not found:" + this._LabelField + "</p>";<br />
flag = false;<br />
}<br />
if (!flag){<br />
goto Label_0868;<br />
}<br />
foreach (SPListItem item in items){<br />
num3++;<br />
try{<br />
int index = 0;<br />
foreach (string str5 in strArray){<br />
string[] strArray7;<br />
IntPtr ptr2;<br />
float num = Convert.ToSingle(item[str5]);<br />
if (num > num2){<br />
num2 = num;<br />
}<br />
if (!string.IsNullOrEmpty(strArray2[index])){<br />
string[] strArray6;<br />
IntPtr ptr;<br />
(strArray6 = strArray2)[(int)(ptr = (IntPtr)index)] = strArray6[(int)ptr] + ",";<br />
}<br />
string str6 = num.ToString().Replace(",", ".");<br />
(strArray7 = strArray2)[(int)(ptr2 = (IntPtr)index)] = strArray7[(int)ptr2] + str6;<br />
index++;<br />
}<br />
}<br />
catch{<br />
str = str + "<p>Data column error:" + this._DataField + "</p>";<br />
}<br />
try{<br />
if (str3 != ""){<br />
str3 = str3 + "|";<br />
}<br />
if ((this._LabelField != "LinkTitle") && (item.Fields[this._LabelField].FieldValueType == typeof(DateTime))){<br />
str3 = str3 + ((DateTime)item[this._LabelField]).ToString("MMM yyyy");<br />
}<br />
else if (item[this._LabelField] != null){<br />
str3 = str3 + item[this._LabelField].ToString();<br />
}<br />
continue;<br />
}<br />
catch{<br />
str = str + "<p>Label column error:" + this._LabelField + "</p>";<br />
continue;<br />
}<br />
}<br />
string str7 = "";<br />
string str8 = "";<br />
string str9 = "";<br />
string str10 = "";<br />
string str11 = "";<br />
string str12 = "";<br />
string str13 = "";<br />
if (!string.IsNullOrEmpty(str3)){<br />
str3 = str3.Replace("&", "%26");<br />
}<br />
string str14 = num2.ToString().Replace(",", ".");<br />
switch (this._ChartType){<br />
case ChartTypes.Pie:<br />
str7 = "p";<br />
str9 = str3;<br />
str12 = "0," + str14;<br />
goto Label_051D;<br />
<br />
case ChartTypes.Pie3D:<br />
str7 = "p3";<br />
str9 = str3;<br />
str12 = "0," + str14;<br />
goto Label_051D;<br />
<br />
case ChartTypes.Line:<br />
str7 = "lc";<br />
str8 = "x,y";<br />
str10 = "0:|" + str3;<br />
str11 = "1,0," + str14;<br />
str12 = "0," + str14;<br />
goto Label_051D;<br />
<br />
case ChartTypes.VerticalBar:<br />
str7 = "bvg";<br />
str8 = "x,y";<br />
str10 = "0:|" + str3;<br />
str11 = "1,0," + str14;<br />
str12 = "0," + str14;<br />
str13 = "a";<br />
goto Label_051D;<br />
<br />
case ChartTypes.HorizontalBar:<br />
str7 = "bhg";<br />
str8 = "x,y";<br />
strArray3 = str3.Split(new char[] { '|' });<br />
str3 = "";<br />
num5 = strArray3.Length - 1;<br />
goto Label_04DE;<br />
<br />
default:<br />
str7 = "p";<br />
goto Label_051D;<br />
}<br />
Label_04AE:<br />
if (str3 != ""){<br />
str3 = str3 + "|";<br />
}<br />
str3 = str3 + strArray3[num5];<br />
num5--;<br />
Label_04DE:<br />
if (num5 >= 0){<br />
goto Label_04AE;<br />
}<br />
str10 = "1:|" + str3;<br />
str11 = "0,0," + str14;<br />
str12 = "0," + str14;<br />
str13 = "a";<br />
Label_051D:<br />
if (str == ""){<br />
object obj2 = str;<br />
str = string.Concat(new object[] { obj2, "?chs=", this._ChartWidth, "x", this._ChartHeight });<br />
if (!string.IsNullOrEmpty(this._ChartTitle)){<br />
string str15 = this._ChartTitle.Replace(" ", "+");<br />
str = str + "&chtt=" + str15.Replace("&", "%26");<br />
}<br />
str = (str + "&cht=" + str7) + "&chts=000000,17" + "&chd=t:";<br />
int num6 = 0;<br />
string[] strArray8 = strArray;<br />
for (int i = 0; i < strArray8.Length; i++){<br />
string text1 = strArray8[i];<br />
if (num6 > 0){<br />
str = str + "|";<br />
}<br />
str = str + strArray2[num6];<br />
num6++;<br />
}<br />
str = str + "&chf=bg,s," + this._BGColor;<br />
if (this._ChartColor != ""){<br />
str = str + "&chco=" + this._ChartColor;<br />
}<br />
if (str9 != ""){<br />
str = str + "&chl=" + str9;<br />
}<br />
if (str8 != ""){<br />
str = str + "&chxt=" + str8;<br />
}<br />
if (str10 != ""){<br />
str = str + "&chxl=" + str10;<br />
}<br />
if (str11 != ""){<br />
str = str + "&chxr=" + str11;<br />
}<br />
if (str12 != ""){<br />
str = str + "&chds=" + str12;<br />
}<br />
if (str13 != ""){<br />
str = str + "&chbh=" + str13;<br />
}<br />
str = str + "&chg=" + ((100.0 / ((double)num3))).ToString("F") + ",20";<br />
if (strArray.Length > 1){<br />
str = str + "&chdl=";<br />
int num7 = 0;<br />
foreach (string str16 in strArray){<br />
if (num7 > 0){<br />
str = str + "|";<br />
}<br />
str = str + str16;<br />
num7++;<br />
}<br />
str = str + "&chdlp=";<br />
switch (this._ChartLegend){<br />
case LegendPositions.horizontal_top:<br />
str = str + "t";<br />
break;<br />
<br />
case LegendPositions.horizontal_bottom:<br />
str = str + "b";<br />
break;<br />
<br />
case LegendPositions.vertical_top:<br />
str = str + "tv";<br />
break;<br />
<br />
case LegendPositions.vertical_bottom:<br />
str = str + "bv";<br />
break;<br />
<br />
case LegendPositions.vertical_left:<br />
str = str + "l";<br />
break;<br />
<br />
case LegendPositions.vertical_right:<br />
str = str + "r";<br />
break;<br />
}<br />
}<br />
str2 = str2 + "<br>" + str.Replace("&", "<br>&");<br />
str = "http://chart.apis.google.com/chart" + str;<br />
}<br />
}<br />
catch{<br />
str = this._ListName + ": Sharepoint List not found!";<br />
}<br />
}<br />
else{<br />
str = "No Sharepoint List defined!";<br />
}<br />
}<br />
catch (Exception){<br />
str = "<br>Site not found:" + this._SiteName;<br />
}<br />
Label_0868:<br />
return str;<br />
}<br />
}<br />
}</div></div><br />
Now change the variable names according to your site, list and columns.<br />
<br />
Then Add this line to your .ascx file.<br />
<script class="brush: csharp;wrap-lines: false" type="syntaxhighlighter">
<![CDATA[
<asp:Image ID="Image1" runat="server" />
]]>
</script><br />
Now replace your "Page_Load" method as follows.<br />
<script class="brush: csharp;wrap-lines: false" type="syntaxhighlighter">
<![CDATA[
protected void Page_Load(object sender, EventArgs e)
{
Chart chart = new Chart();
string imgurl = chart.createChartString("CHWP6");
Image1.ImageUrl = imgurl;
}
]]>
</script><br />
Now you can built and deploy your web part to see the result. You can get VerticalBar,Pie,Pie3D and HorizontalBar charts by changing the "ChartType" in the above code.<br />
<br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com1tag:blogger.com,1999:blog-790956358810066267.post-69168988525564651622009-11-13T15:44:00.009+05:302010-09-05T22:42:36.412+05:30Create SharePoint list programmatically C#<div style="font-family: Verdana,sans-serif;"><br />
In this article I’m going to discuss how we can create a SharePoint list programmatically in c# and how we can add columns to the created list. Actually this is very simple task; this is a sample code. <br />
<pre class="brush: csharp"> public void createList()
{
// choose your site
SPSite site = new SPSite("http://merdev-moss:5050");
SPWeb web = site.OpenWeb();
SPListCollection lists = web.Lists;
// create new Generic list called "My List"
lists.Add("My List", "My list Description", SPListTemplateType.GenericList);
SPList list = web.Lists["My List"];
// create Text type new column called "My Column"
list.Fields.Add("My Column", SPFieldType.Text, true);
// make new column visible in default view
SPView view = list.DefaultView;
view.ViewFields.Add("My Column");
view.Update();
}
</pre>In the above code I have created Generic list and normal Text column. You can use whatever list type and field type according to your requirement. To learn more about SharePoint lists, follow "<a href="http://sarangasl.blogspot.com/2009/09/sharepoint-list-c-part-1.html">SharePoint List C# Part 1</a>".<br />
<br />
If you want to add lookup columns please refer "<a href="http://sarangasl.blogspot.com/2009/11/add-lookup-column-to-sharepoint-list.html">Add Lookup Column to SharePoint List programmatically</a>".<br />
<br />
To learn how to create List View, refer "<a href="http://sarangasl.blogspot.com/2009/12/create-sharepoint-list-view.html">Create SharePoint List View Programmatically</a>".<br />
<br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com3tag:blogger.com,1999:blog-790956358810066267.post-19037391233781325752009-11-10T17:21:00.002+05:302010-03-14T12:29:57.834+05:30Use Custom Workflow Actions in SharePoint Designer<div style="font-family: Verdana,sans-serif;"><br />
In my previous article on "<a href="http://sarangasl.blogspot.com/2009/11/sharepoint-workflow-actions-for.html">SharePoint Workflow Actions for Designer in Visual Studio</a>" I discussed how to create and deploy a custom workflow action to use in the SharePoint designer.In this article I’m going to discussed how you can use the added action in the SharePoint Designer.<br />
<br />
First open your site in the SharePoint Designer and create new workflow as shown in following figure.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNsWvGA8TX0-9FfMl5n9X_NfpIml2GVKW_ZGd4xSTLwRHEHJTWjV-dCEfUrH1IHEPY6H31kKby0BEDm4TN7VN8zZ4rc7FDgRWyOeoVhFjGv7UHZOhSxhXK08wFLZrhoUIZlx-Cm6nJ3WiW/s1600-h/new_workflow.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="new workflow" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNsWvGA8TX0-9FfMl5n9X_NfpIml2GVKW_ZGd4xSTLwRHEHJTWjV-dCEfUrH1IHEPY6H31kKby0BEDm4TN7VN8zZ4rc7FDgRWyOeoVhFjGv7UHZOhSxhXK08wFLZrhoUIZlx-Cm6nJ3WiW/s320/new_workflow.JPG" /></a><br />
</div><br />
Then select the SharePoint list that the workflow should attached to and selects the start option of the workflow as following figure.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjulBcdg7ak4cGc0j8dIci4e5oLuQmHf60JVQZYwRHDSsDabydok95VHNaLW4a7tPa0qc0DGCdmFyFF8WzRHUG5plk7c8xv6VjuL1tPXcfQdwdLsVXm5EtJ2XPzHrZGO4W0y9B98PND8WHQ/s1600-h/give_workflow_start_option.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjulBcdg7ak4cGc0j8dIci4e5oLuQmHf60JVQZYwRHDSsDabydok95VHNaLW4a7tPa0qc0DGCdmFyFF8WzRHUG5plk7c8xv6VjuL1tPXcfQdwdLsVXm5EtJ2XPzHrZGO4W0y9B98PND8WHQ/s320/give_workflow_start_option.JPG" /></a><br />
</div><br />
Now you can find the newly deployed custom action in the "Extras" category. Chose it and click the "Add" button. Then you have to specify the parameters if your custom action asked for them as follow.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjS_qXKe84dfGcq9NLLfeg605HQLHgW5D_kcRr1qjF54IvSkZVcpgcLOJqAfWnIyjWTXCyzZwWHpscYaqcs6gmoqkNCzJcgto6aaqt4tsfPPoLgldTR6KamK6X0u9s2arK_RGtDRegTzlW/s1600-h/add_custom_workflow_action.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjS_qXKe84dfGcq9NLLfeg605HQLHgW5D_kcRr1qjF54IvSkZVcpgcLOJqAfWnIyjWTXCyzZwWHpscYaqcs6gmoqkNCzJcgto6aaqt4tsfPPoLgldTR6KamK6X0u9s2arK_RGtDRegTzlW/s320/add_custom_workflow_action.JPG" /></a><br />
</div><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCgQclvVvTmTgf_tiWoP9WFe3x3Toj-r3BpLFtaQpm6Ry4TYznvoviZnh5iq_q-KoIGIlbK0a5jl2U-ZmaRfrRyl19RtK8rP1T-DjRs60CH4Q-1XceeI0mcVx0IGRhX1Avk2pri4fo4doH/s1600-h/give_workflow_parameters.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCgQclvVvTmTgf_tiWoP9WFe3x3Toj-r3BpLFtaQpm6Ry4TYznvoviZnh5iq_q-KoIGIlbK0a5jl2U-ZmaRfrRyl19RtK8rP1T-DjRs60CH4Q-1XceeI0mcVx0IGRhX1Avk2pri4fo4doH/s320/give_workflow_parameters.JPG" /></a><br />
</div><br />
Now everything is done and you can test your work flow. To do this, go to selected list and click on the arrow appear when you put the mouse on title of a list item. Then select the "Workflows" option.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrbtIwsH490fQCGyNuklZedpJX-gcwJ0PrnJcGcYlGN6CP2qHVT7z-K_9IUc_HzrNkp2UAESHSfuKii1w9fNhzHNyDJb_8mFyX5uPVfk4bYW8fUnuD9Mar-Pp418YD5B5bugytvks88xTv/s1600-h/check_workflow_status.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrbtIwsH490fQCGyNuklZedpJX-gcwJ0PrnJcGcYlGN6CP2qHVT7z-K_9IUc_HzrNkp2UAESHSfuKii1w9fNhzHNyDJb_8mFyX5uPVfk4bYW8fUnuD9Mar-Pp418YD5B5bugytvks88xTv/s320/check_workflow_status.JPG" /></a><br />
</div><br />
Follow my article on "<a href="http://sarangasl.blogspot.com/2009/11/sharepoint-workflow-actions-for.html">SharePoint Workflow Actions for Designer in Visual Studio</a>" to learn how to create and deploy a custom workflow action to use in the SharePoint designer.<br />
<br />
</div>Saranga Rathnayakehttp://www.blogger.com/profile/01103209861183518579noreply@blogger.com2