Friday, April 27, 2012

Wcf Ria Service using in silverlight Application

Steps for creating such an application are below.
Create New Silverlight Application

New project >> Select Silverlight Application.



Check "Enable WCF RIA Services" check box










Create Entity Data model.







Select tables, views and stored procedure from given list to add it to the entity data model.



Create Domain Service.

Before adding a new domain service, compile the web project.


Map or enable entity(s) with domain service. In the Entities list you get all tables (also called entities) available in the entity model.

  
Now build both projects; Silverlight as well as web. After compiling you will have auto generated code in the Silverlight project which is responsible for communication between Silverlight and Domain service.



Domain service has many built-in methods for selecting, inserting, updating and deleting the entity(s). You may also add new methods as per your requirement.
In the attached Silverlight project we a create custom method to get customer from its id.

public CustomerMaster GetCustomerFromId(int customerId)
{
    return this.ObjectContext.CustomerMasters.Where(d => d.CustomerId == customerId).FirstOrDefault();
}

After completion of the web project, you may access this method in Silverlight project.

Binding Data using WCF service in silver light 4

Using simple LINQ query and LINQ to sql class.
Open Project in visual studio 2010.
Right click on <Projects>.web Project and select
add new item
select linq to sql classes.Name as Exercise.
Select your database, tables, stored procedures if required.
Right click on <Projects>.web Project and select
add new item
select silverlight Tab and select silverlight enabled wcf service.Name as WCFexample.
Add the following code to get data from table


[OperationContract]
        List<nvmsl_ClientUser> GetUsers()
        {
            //Creating object for Exercise.dbml.cs class's ExerciseDataContext
            ExerciseDataContext dc = new ExerciseDataContext();

            var users = from user in dc.nvmsl_ClientUsers select user;
            return users.ToList();

        }
Test the webservice:
To test the web service, reight click on service and select view in browser.
Copy the link.
Open visual studio 2010 command prompt from visual studio 2010 tools.
Type wcftestclient and enter.
One window will open, right click on My Project services from the right side of the window.
Select add service
Paste the link and click add.
Select the required function and select invoke.
Testing completed successfully.

Adding service reference.
Right click on <project> select add service reference.
Select discover.
Select required service and add namespace.

Add a silver light page

-> And add datagrid control name it as userdatagrid, ser Autogenerated column property = True.
->And add the following code in page to bind data to userdatagrid.

public <Pageconstructor>()
        {
            InitializeComponent();

            WCFexampleService.WCFexampleClient ws = new WCFexampleService.WCFexampleClient();
            ws.GetUsersCompleted += new EventHandler<WCFexampleService.GetUsersCompletedEventArgs>(ws_GetUsersCompleted);
            ws.GetUsersAsync();
        }

        void ws_GetUsersCompleted(object sender, WCFexampleService.GetUsersCompletedEventArgs e)
        {
            UserdataGrid.ItemsSource = e.Result;
        }


Run the application.


Using ADO.Net Entity Data Model, simple LINQ Query.
Open Project in visual studio 2010.
Right click on <Projects>.web Project and select
add new item
select ADO.net Entity Data Model.Named WCFexampleModel. Give the connection Details and name it as NitsWebSLEntities.
Select required tables and procedures.
Build.



 Right click on <Projects>.web Project and select
add new item
select silverlight Tab and select silverlight enabled wcf service.Name as WCFexample.
Add the following code to get data from table


[OperationContract]
        List<nvmsl_ClientUsers> GetUsers()
        {

NitsWebSLEntities dc = new NitsWebSLEntities();
            var users = from user in dc.nvmsl_ClientUsers select user;
            return users.ToList();

        }
Test the webservice:
To test the web service, reight click on service and select view in browser.
Copy the link.
Open visual studio 2010 command prompt from visual studio 2010 tools.
Type wcftestclient and enter.
One window will open, right click on My Project services from the right side of the window.
Select add service
Paste the link and click add.
Select the required function and select invoke.
Testing completed successfully.

Adding service reference.
Right click on <project> select add service reference.
Select discover.
Select required service and add namespace.

Add a silver light page

-> And add datagrid control name it as userdatagrid, ser Autogenerated column property = True.
->And add the following code in page to bind data to userdatagrid.

public <Pageconstructor>()
        {
            InitializeComponent();

            WCFexampleService.WCFexampleClient ws = new WCFexampleService.WCFexampleClient();
            ws.GetUsersCompleted += new EventHandler<WCFexampleService.GetUsersCompletedEventArgs>(ws_GetUsersCompleted);
            ws.GetUsersAsync();
        }

        void ws_GetUsersCompleted(object sender, WCFexampleService.GetUsersCompletedEventArgs e)
        {
            UserdataGrid.ItemsSource = e.Result;
        }





Using ADO.Net Entity Data Model, Stored procedure.( Returns a collection of : Entities.)
Open Project in visual studio 2010.
Right click on <Projects>.web Project and select
add new item
select ADO.net Entity Data Model.Named WCFexampleModel. Give the connection Details and name it as NitsWebSLEntities.
Select required tables and procedures.
Build.
Create a stored procedure named Tnvmsl_GetClientUsers.
CREATE PROCEDURE [dbo].[Tnvmsl_GetClientUsers]
AS
BEGIN
select * from dbo.nvmsl_ClientUsers
    END

Right click on WCFexampleModel.edmx select model browser.
 From the left corner Tree view select Tnvmsl_GetClientUsers Procedure in stored procedures in NitsWebSLModel.Store.
Right click on Tnvmsl_GetClientUsersselect Add function Import.
Select Returns a collection of : Entities.


Add the following code in the service
    [OperationContract]
        List<nvmsl_ClientUsers> Get_ClientUsers()
        {
            NitsWebSLEntities dc = new NitsWebSLEntities();
            return dc.Tnvmsl_GetClientUsers().ToList();
        }

Add the following code in Silverlight page.

WCFexampleService.WCFexampleClient ws = new WCFexampleService.WCFexampleClient();
            ws.Get_ClientUsersCompleted += new EventHandler<WCFexampleService.Get_ClientUsersCompletedEventArgs>(ws_Get_ClientUsersCompleted);
            ws.Get_ClientUsersAsync();


        void ws_Get_ClientUsersCompleted(object sender, WCFexampleService.Get_ClientUsersCompletedEventArgs e)
        {
            UserdataGrid.ItemsSource = e.Result;
        }






Using ADO.Net Entity Data Model, Stored procedure.( Returns a collection of : Scalars.)

Open Project in visual studio 2010.
Right click on <Projects>.web Project and select
add new item
select ADO.net Entity Data Model.Named WCFexampleModel. Give the connection Details and name it as NitsWebSLEntities.
Select required tables and procedures.
Build.
Create a stored procedure named Tnvmsl_GetUserName.
ALTER PROCEDURE [dbo].[Tnvmsl_GetUserName]
(@UserId int)
AS
BEGIN
select UserName from dbo.nvmsl_ClientUsers where UserId=@UserId
END

Right click on WCFexampleModel.edmx select model browser.
 From the left corner Tree view select Tnvmsl_GetUserName Procedure in stored procedures in NitsWebSLModel.Store.
Right click on Tnvmsl_GetUserName Add function Import.
Select Returns a collection of : Scalars

Add the following code in the service
  [OperationContract]
        List<string> Get_UserNameByUserID(int id)
        {
            NitsWebSLEntities dc = new NitsWebSLEntities();
            return dc.Tnvmsl_GetUserName(id).ToList();

        }


Add the following code in Silverlight page.
WCFexampleService.WCFexampleClient ws = new WCFexampleService.WCFexampleClient();
            ws.Get_UserNameByUserIDCompleted += new EventHandler<WCFexampleService.Get_UserNameByUserIDCompletedEventArgs>(ws_Get_UserNameByUserIDCompleted);
            ws.Get_UserNameByUserIDAsync(1);

void ws_GetUsersCompleted(object sender, WCFexampleService.GetUsersCompletedEventArgs e)
        {
            UserdataGrid.ItemsSource = e.Result;
        }


Using ADO.Net Entity Data Model, Stored procedure.( Returns a collection of : Complex Types.)
Open Project in visual studio 2010.
Right click on <Projects>.web Project and select
add new item
select ADO.net Entity Data Model.Named WCFexampleModel. Give the connection Details and name it as NitsWebSLEntities.
Select required tables and procedures.
Build.
Create a stored procedure named nvmsl_GetClient.
ALTER PROCEDURE [dbo].[nvmsl_GetClient]
@ClientID int,
@Active bit
AS
BEGIN
    select c.ClientID,cu.UserName,cu.Password,cu.Email,cu.PasswordQuestion,cu.PasswordAnswer from dbo.nvmsl_ClientUsers cu,dbo.nvm_Client c where c.ClientID=@ClientID and c.Active=@Active and c.ClientID=cu.ClientID
END


Right click on WCFexampleModel.edmx select model browser.
 From the left corner Tree view select nvmsl_GetClient Procedure in stored procedures in NitsWebSLModel.Store.
Right click on nvmsl_GetClient Add function Import.
Select Returns a collection of : Complex
Click the button Get column information.
And click the button Create new complex types.
You will get a name “nvmsl_GetClient_Result” after clicking the above button.

Add the following code in the service

        [OperationContract]
        List<nvmsl_GetClient_Result> Get_Clients(int id, bool status)
        {
            NitsWebSLEntities dc = new NitsWebSLEntities();
            return dc.nvmsl_GetClient(id, status).ToList();

        }


Add the following code in Silverlight page.
          WCFexampleService.WCFexampleClient ws = new WCFexampleService.WCFexampleClient();
            ws.Get_ClientsCompleted += new EventHandler<WCFexampleService.Get_ClientsCompletedEventArgs>(ws_Get_ClientsCompleted);
            ws.Get_ClientsAsync(6,true);


void ws_GetUsersCompleted(object sender, WCFexampleService.GetUsersCompletedEventArgs e)
        {
            UserdataGrid.ItemsSource = e.Result;
        }

How to use Asp.net Membership use in silverlight Business application

Reference: http://msdn.microsoft.com/en-us/gg315205

Open the project in visual studio 2010
Open the  <project>.web projects web.config file.
Find the <system.web> element
Delete the <rolemanager Enabled=”true”> element
Delete the <profile> element and all its children.
Inside the <system.web> element, add the following.
<membership defaultProvider="SlEventManagerMembershipProvider" userIsOnlineTimeWindow="15">
  <providers>
    <clear />
    <add name="SlEventManagerMembershipProvider"
         type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
         connectionStringName="SlEventManagerDb" applicationName="/SlEventManager" enablePasswordRetrieval="false"
         enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="true"
         passwordFormat="Hashed" />
  </providers>
</membership>

<roleManager enabled="true" defaultProvider="SlEventManagerRoleProvider">
  <providers>
    <clear />
    <add name="SlEventManagerRoleProvider"
         type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
         connectionStringName="SlEventManagerDb" applicationName="/SlEventManager" />
  </providers>
</roleManager>

<profile enabled="true" defaultProvider="SlEventManagerProfileProvider">
  <providers>
    <clear />
    <add name="SlEventManagerProfileProvider" connectionStringName="SlEventManagerDb"
         applicationName="/SlEventManager"
         type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </providers>
  <properties>
    <add name="FriendlyName" />
  </properties>
</profile>
Find <connectionStrings> section of the web.config.add another entry for the authentication database.

<add name="SlEventManagerDb"
     connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SlEventManager.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"
     providerName="System.Data.SqlClient" />
Run the application.

Displaying links for user based on login
Write the following in MainPage’s constructor

  WebContext.Current.Authentication.LoggedIn += (se, ev) =>
            {
                Link3.Visibility = System.Windows.Visibility.Visible;
                Divider2.Visibility = Visibility.Visible;
            };
            WebContext.Current.Authentication.LoggedOut += (se, ev) =>
            {
                Link3.Visibility = System.Windows.Visibility.Collapsed;
                Divider2.Visibility = Visibility.Collapsed;
            };
If user logged out, user should be redirected to Home or Particular page

            WebContext.Current.Authentication.LoggedOut += (se, ev) =>
            {

                NavigationService.Navigate(new Uri("/Login", UriKind.Relative));
            };


Sharepoint Interview Questions

  1. What is SharePoint?
  2. What is SharePoint architure?
  3. What is a Portal Technology?
  4. What's new in SharePoint 2010?
  5. How is SharePoint foundation 2010 differs from SharePoint Server 2010?
  6. What is Site provisioning?
  7. What is a SharePoint farm?
  8. What is Site and Site Collection?
  9. How to create a site using stsadm.exe?
  10. What is Customization and Personalization?
  11. What is SharePoint Feature and how to create and deploy a feature?
  12. What is Visual web part? And how to create and deploy a visual webpart using visual studio 2010?
  13. What is the work of SPVirtualPathProvider?
  14. What is ghosting and unghosting pages?
  15. What is difference between Site Pages versus Application Pages?
  16. How to create Custom Application Pages?
  17. How to use SPGridView in application pages?
  18. How to add a menu item to custom action menu?
  19. SharePoint object model: SPFile,SPFolder
  20. What is ghosted and unghosted pages?
  21. How to use usercontrols in SharePoint?
  22. How to design a SharePoint web part page?
  23. What is master page? What are the default master pages available in SharePoint 2010?
  24. How to create Master page using SharePoint designer and Visual studio 2010?
  25. What are SharePoint delegate controls?
  26. How to use css files in SharePoint 2010?
  27. How to use content editor web part?
  28. How to use Data view webpart?
  29. How to use list view web part?
  30. How to use Page viewer web part to display a web page?
  31. What is a web part page life cycle?
  32. What is the difference between web part inherited from  System.Web.UI.WebControls.WebParts.WebPart class and Microsoft.SharePoint.WebPartPages.WebPart  class?
  33. How to deploy web part into SharePoint ?
  34. What is Web Part Verbs?
  35. What is a class resource file?
  36. How to retrieve list items using object model?
  37. What is CAML? And how to use CAML?
  38. How to define Custom list elements with CAML?
  39. What is difference between SPQuery and SPSiteDataQuery ?
  40. What is a Site column?
  41. How to create a site column using browser and CAML?
  42. What is a look up column?
  43. What is a Field type and How to create a custom field type using CAML?
  44. What is a content type?
  45. How to create a content type using Visual Studio 2010?
  46. What are event receivers, What are different types of event receivers in SharePoint 2010?
  47. How to create an event receiver and how to attach the event receiver to list in SharePoint 2010?
  48. What is the difference between a document library and a list?
  49. What is the difference between a form library and a document library?
  50. How to check the version number of documents inside a document library using SharePoint object model?
  51. How to programmatically add a document to a document library?
  52. What is configuration database?
  53. What is a content database?
  54. What is the improvements in SharePoint 2010 central administration?
  55. What is service application and how it is different from Shared Service providers?
  56. What is user profile service ?
  57. What is my site?
  58. How is power shell different from stsadm?
  59. What are the enhancements in Visual Studio 2010 for SharePoint 2010?
  60. How to create feature in Visual studion 2010?
  61. What is the use of feature.xml file and elements.xml file?
  62. How to deploy a visual web part in SharePoint 2010?
  63. What are mapped folders in SharePoint 2010?
  64. What is a Sandboxed solution?
  65. What is a site template ?
  66. What is the difference between site template and site defination?
  67. What is a site pages and an application pages?
  68. What is a ghosted page and unghosted pages?
  69. How to create a page from a master page in SharePoint designer?
  70. How to deploy a custom master page in SharePoint?
  71. How to create user control? And how to add user controls to pages? 
  72. What is a web part manager?
  73. How to make a custom editor parts in SharePoint 2010?
  74. What is a web part verbs?
  75. How to work with connected web part?
  76. What are event receiver?
  77. How to attach event receivers to a list in SharePoint 2010?
  78. What is ONET.xml file?
  79. What is feature stapling?
  80. How to query a list using CAML?
  81. How to Query multiple lists?
  82. How to use linq in SharePoint 2010?
  83. Clinet object model
  84. How to work with Silver light client object model?
  85. How to work with Java Script client object model?
  86. How to work with WCF data services?
  87. What is a workflow?
  88. What are new out of box workflows in SharePoint 2010?
  89. What is a document sets?
  90. What is a Site workflow and what is a reusable workflow?
  91. What is a sequential workflow?
  92. What is a state machine workflow?
  93. How to deploy a workflow in SharePoint 2010?
  94. Advantages of Power shell over stsadm?
  95. How to take back up in SharePoint 2010?
  96. How FAST search works in SharePoint 2010?
  97. How Authentication & Authorization works in SharePoint 2010?
  98. What is claim baised authentication?
  99. What is SPSecurity.RunWithElevatedPrivileges?
  100. What is SHAREPOINT\SYSTEM account does?
  101. What are different SharePoint site roles available in SharePoint 2010?
  102. SPUtility class?
  103. What is enterprise content management?
  104. What is web content management?
  105. How to do EMail settings in SharePoint 2010?