MVVM (Model-View-ViewModel)

The Model View ViewModel (MVVM) is an architectural pattern used in software engineering.

It originated from Microsoft as a specialization of the Presentation Model design pattern introduced by Martin Fowler.

Largely based on the Model-view-controller pattern (MVC), MVVM is targeted at modern UI development platforms.

MVVM was designed to make use of specific functions in WPF to better facilitate.

The separation of View layer development from the rest of the pattern by removing virtually all “code behind” from the View layer.

Instead of requiring Interactive Designers to write View code, they can use the native WPF markup language XAML and create bindings to the ViewModel, which is written and maintained by application developers.

This separation of roles allows Interactive Designers to focus on UI needs rather than programming or business logic, allowing for the layers of an application to be developed in multiple work streams.

 

Model

  1. The Model represents the actual data and/or information we are dealing with.An example of a model might be a contact (containing name, phone number, address, etc.) or it can be the Web service , Data Access in short anything which provide the data.
  2. The key to remember with the model is that it holds the information.

 

ViewModel

  1. ViewModel is the base in the MVVP pattern.Acts as a “View” specific model class,adapting your model to the need of the view.
  2. It hold the state of the view.but do not know or hold the reference of the view.View is responsible for hold the reference of the ViewModel. 
  3. Responsible for talking with model/Service through their interfaces.
  4. The viewmodel may expose the model directly, or properties related to the model, for data-binding
  5. The ViewModel can also Validate the Data.
  6. The viewmodel exposes not only models, but also exposes Notifiable Properties, and Observable Collections to the View.The View Binds to these ViewModel entities/members(supported by wpf).

View

  1.  The view is what most of us are familiar with and the only thing the end user really interacts with. It is the presentation of the data.
  2. The View holds a reference to the ViewModel.View is completely isolated from the model.
  3. In MVVM ,A View is defined in XAML and should not have any logic in the code-behind. It binds to the view-model by only using data binding.
  4. The view handles its own UI events, then maps them to the viewmodel via commands.

General

  1. The view and the viewmodel communicate via data-binding, method calls, properties, events, and messages.
  2. The models and properties on the viewmodel are updated from the view via two-way databinding.
  3. When user update the view,Databinding pushes changes to the ViewModel.
  4. when Model updates ViewModel DataBinding pushes changes to the View. 
  5. The key thing to remember is that you have one and only one ViewModel per view.

Implementations of the MVVM pattern have the following characteristics:

  1. The View class generates events in response to user interactions, and these events are handled by the corresponding ViewModel class. The View class has no knowledge of how the events are handled, or what impact the events will have on the Model.
  2. The ViewModel class determines whether a user action requires modification of the data in the Model, and acts on the Model if required. For example, if a user presses a button to update the inventory quantity for a part, the View simply notifies the ViewModel that this event occurred. The ViewModel retrieves the new inventory amount from the View and updates the Model. This decouples the View from the Model, and consolidates the business logic into the ViewModel and the Model where it can be tested.
  3. The Model notifies the ViewModel if the data in the underlying data store has changed. Generally, when you work with a stateless request/response model, you don’t need to worry about whether data has changed while the request is being processed, since the window of time is small. With rich Internet application (RIA) approaches, the Model data typically stays in memory for longer, and multiple active Views may share the Model data. A user may make changes in one View that affects a different View within the application. The Model fires events to notify any active ViewModels of data changes.
  4. The ViewModel notifies the View when information has changed. This is typically automated through the two-way binding infrastructure described previously.

MVVM

Benefits and Consequences of Using MVVM

The benefits of MVVM are the following:

  1. Loose Coupling.
  2. A ViewModel provides a single store for presentation policy and state, thus improving the reusability of the Model (by decoupling it from the Views) and the replaceability of the Views (by removing specific presentation policy from them)
  3. The Model-View-ViewModel design improves the overall testability of the application. One can easily create unit tests that target the Model and the ViewModel layers. The MVVM design also improves the “mockability” of the application, by allowing easy run-time simulation of the tiers, which is crucial to testing complex software products.
  4. The Model-View-ViewModel design is a very loosely coupled design. The View holds a reference to the ViewModel, and the ViewModel holds a reference to the Model. The rest is done by the data-binding and commanding infrastructure of WPF.

The consequences of MVVM are the following:

  1. Hard to Dabug because Databinding is  Declarative.
  2. The typical relationship between a ViewModel and the corresponding Views is one-to-many, but there are also situations where that is not necessarily true. In general, any business logic and input-handling business logic (selection tracking, and so on) is kept in the ViewModel.
  3. There are situations when one ViewModel is aware of another ViewModel within the same application. Such situations arise when there is a master-subordinate relationship between two ViewModels or when a ViewModel represents a single item (for example, the visual representation state of a single contact). When this happens, one ViewModel can represent a collection of ViewModels.

Othere Reference :http://msdn.microsoft.com/en-us/library/ff798384.aspx

SharePoint’s type of List & its Item’s Field

Calendar List

Field Name Type
Location String
Start Time DateTime
End Time DateTime
Description String
Title String
Category String
fAllDayEvent Boolean
   
   

Contact List

Field Name Type
Title String
FirstName String
FullName String
Email String
Company String
JobTitle String
WorkPhone String
HomePhone String
CellPhone String
WorkFax String
WorkAddress String
WorkCity String
WorkState String
WorkZip String
WorkCountry String
WebPage String
Comments String

Announcement List

Field Name Type
Title String
Body String
Expires DateTime

Disscussion List

Field Name Type
Title String
Body String

Task List

Field Name Type
Title String
Status String
PercentComplete String
Priority String
Body String
StartDate DateTime
DueDate DateTime
AssignedTo user

Issue List

Field Name Type
Title String
Status String
Comments String
Priority String
Description String
DueDate DateTime

Link List

Field Name Type
URL SPFieldUrlValue
Notes String

Programmatically add an item/Contact to a SharePoint 2010 Contact List

Here,in this post I am going to describes how we can programmatically add an contact to the SharePoint 2010.

We go here step by steps.So.let’s begin.

(1) Creating the Contact List.

//Get  the Site under which you want to create Contacts.

SPSite site = new SPSite(http://localhost);
SPWeb web = site.AllWebs[0];

//Give the Type of List you want to create,in Our case it’s noting but Contacts.
SPList list = web.Lists["Contacts"];

Now,Up to this we has the instance of Contact list.

(2)Adding Contact In to Contact list.

To Add/Create the Contacts we need to update following field.

Field Name Type
Title String
FirstName String
FullName String
Email String
Company String
JobTitle String
WorkPhone String
HomePhone String
CellPhone String
WorkFax String
WorkAddress String
WorkCity String
WorkState String
WorkZip String
WorkCountry String
WebPage String
Comments String

//Adding event into list
  SPListItem contact = list.Items.Add();

contact["Title"] = “Rajput”;

contact["FirstName"] = “Chirag”;

contact["FullName"] = “Rajput Chirag”;

contact["Email"] =  “Chirag@test.com”;

contact["Company"] = “My Company”;

contact["JobTitle"] = “Software Eng.;

contact["WorkPhone"] = “0303- 1233344” ;

contact["HomePhone"] = “2121-323232”;

contact["CellPhone"] = “+1213232323”;

contact["WorkFax"] = “122132-3232”

contact["WorkAddress"] = “ddadadada”;

contact["WorkCity"] = “Bangalore”;

contact["WorkState"] = “Karnataka”;

contact["WorkZip"] = “1212-32”;

contact["WorkCountry"] = workcountry" ;

contact["WebPage"] = “http://Mylog.com”;

contact["Comments"] = “MyComments”;

contact.Update();

Your,final code will look like this.

//Get  the Site under which you want to create Contacts.

SPSite site = new SPSite(http://localhost);
SPWeb web = site.AllWebs[0];

//Give the Type of List you want to create,in Our case it’s noting but Contacts.
SPList list = web.Lists["Contacts"];

//Adding Contact into list
  SPListItem contact = list.Items.Add();

contact["Title"] = “Rajput”;

contact["FirstName"] = “Chirag”;

contact["FullName"] = “Rajput Chirag”;

contact["Email"] =  “Chirag@test.com”;

contact["Company"] = “My Company”;

contact["JobTitle"] = “Software Eng.;

contact["WorkPhone"] = “0303- 1233344” ;

contact["HomePhone"] = “2121-323232”;

contact["CellPhone"] = “+1213232323”;

contact["WorkFax"] = “122132-3232”

contact["WorkAddress"] = “ddadadada”;

contact["WorkCity"] = “Bangalore”;

contact["WorkState"] = “Karnataka”;

contact["WorkZip"] = “1212-32”;

contact["WorkCountry"] = workcountry" ;

contact["WebPage"] = “http://Mylog.com”;

contact["Comments"] = “MyComments”;

contact.Update();

Thanks.

Programmatically add an item/event to a SharePoint 2010 calendar

Here,in this post I am going to describes how we can programmatically add an Event to the SharePoint 2010.

In SharePoint, Calendar is nothing but the one type of list.So as we are adding item in it,we can create the event for Calendar.

We go here step by steps.So.let’s begin.

(1) Creating the Calendar.

//Get  the Site under which you want to create Calendar.

SPSite site = new SPSite(http://localhost);
SPWeb web = site.AllWebs[0];

//Give the Type of List you want to create,in Our case it’s noting but Calendar.
SPList list = web.Lists["Calendar"];

Now,Up to this we has the instance of Calendar list.

(2)Adding Event In to Calendar.

To Add/Create the calendar we need to update following field.

Field Name    Type    
Location String      
Start Time DateTime      
End Time    DateTime 
Description    String      
Title    String      
Category    String    
fAllDayEvent    Boolean

 

//Adding event into list
  SPListItem newEvent = list.Items.Add();

newEvent["Location"] = “This is location”;

newEvent["Start Time"] = DateTime.Now;

newEvent["End Time"] = DateTime.Now.AddHours(1);

newEvent["Description"] = “Going for Meting”;

newEvent["Title"] = “New Meting”;

newEvent["fAllDayEvent"]  =  false;

newEvent["Category"] = “Meting”;
 

//Create Event in the sharpoint.        
newEvent.Update();

Your,final code will look like this.

//Get  the Site under which you want to create Calendar.

SPSite site = new SPSite(http://localhost);
SPWeb web = site.AllWebs[0];

//Give the Type of List you want to create,in Our case it’s noting but Calendar.
SPList list = web.Lists["Calendar"];

//Adding event into list
  SPListItem newEvent = list.Items.Add();

newEvent["Location"] = “This is location”;

newEvent["Start Time"] = DateTime.Now;

newEvent["End Time"] = DateTime.Now.AddHours(1);

newEvent["Description"] = “Going for Meting”;

newEvent["Title"] = “New Meting”;

newEvent["fAllDayEvent"]  =  false;

newEvent["Category"] = “Meting”;

//Create Event in the sharpoint.        
newEvent.Update();

Enjoy your Calendar Event.



    
     

           
        


 

Sending Mail in SharePoint

There is two way of sending the mail.
(1)using existing .net classes. (System.Net.Mail).
(2)using the SharePoint classes(Microsoft.SharePoint.Utilities).

The benefit of the second choice is you don’t need to store any other configuration for your SMTP server on your code.
The code will directly use the SharePoint configuration.

Configure the SharePoint for the sending the email

So.let’s first see,How we can configure the SharePoint for the sending the email.
To configure the SharePoint
(1)Open/Brows the  "SharePoint Central Administration Site".


(2) In Central Administration’s Menu click on the System Settings.

System settings

(3)Now,in right side click on the Configure outgoing e-mail settings under E-mail and Text Messages(SMS) section.

E-mail

(4)Now, in Outbound SMTP server give the name of the outgoing server address. e.g(smtp.provider.com)

(5)In From address filed give the from address( This can be overwrite by code,it’s ok if you keep this blank). and  click on ok.

Now,your SharePoint is configure to send the mail.

Sending the Mail

Below code will explain you how you can send the mail using SharePoint configuration.

This code is uses classes form the Microsoft.SharePoint.dll and Microsoft.SharePoint.Utilities.dll.

        public bool SendMail()
        {
                //Get Current website
                SPWeb web = SPContext.Current.Web;

               //Check emilServer is set
                bool isEmailServerSet = SPUtility.IsEmailServerSet(web);

                bool result = false;
                if (isEmailServerSet)
                {
                    //The email header
                    StringDictionary headers = new StringDictionary();
                    headers.Add("to","chiragbrajput@hotmail.com"));
                    headers.Add("cc", Chiragbrajput@hotmail.com);


                  //Change the from setting(optional)
                    headers.Add("from","hotmailAdmin@hotmail.com");
                    headers.Add("subject", "TestingEmail");
                    headers.Add("content-type", "text/html"); //This is the default type, so isn’t neccessary.

                    //The email body
                    string bodyText = "hello World";
                   //indicate mail send succefully or not.
                    result = SPUtility.SendEmail(web, headers, bodyText);
                }
    }

How to reduce the flicker in IE on postback ?

Asp.net causes browsers / webpages to flash or flicker during a postback to the server. To reduce this flicker you should include below meta tag inside the header tag. 

<meta http-equiv="Page-Exit" content="Alpha(opacity=100)" />

Below is the Definition and Usage of the Tag/Value

http-equiv

The http-equiv attribute provides an HTTP header for the information in the content attribute.The http-equiv attribute can be used to simulate an HTTP response header.The value of the http-equiv attribute depends on the value of the content attribute.If the name attribute is set, the http-equiv attribute should not be set.

Page-Exit

defines what filter to apply when user leaves the page.

Content

The required content attribute specifies the content of the meta information.The value of the content attribute depends on the value of the name or http-equiv attribute.

Reporting Services Error

Error Message.

SQL Server 2008 RC 0 Setup shows the following error messages after running a set of rules to determine if the installation process will be blocked:

Troubleshooting028-01

Cause.
Setup program found the Reporting Services Catalog database file and Reporting Services Catalog temporary database file in the following path:
C:\ProgramFiles\MicrosoftSQLServer\MSSQL10.MSSQLSERVER\MSSQL\DATA

Solution.
Remove the following files from the "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA" directory:
bulletReportServer.mdf
bulletReportServer_log.LDF
bulletReportServerTempDB.mdf
bulletReportServerTempDB_log.LDF.

Troubleshooting028-02

 

After deleting the files, click on the "Re-run" button of the Installation Rules setup page, and click "Next" after passing all the installation rules.

SQL Server 2008 Installation

In this post i am going to explain the steps for installing the sql server 2008 which include Reporting service installation & it’s  configuration also.

Before you start  installation following requirement should be met.

Prerequisites :

Windows 2003

If you want to install Sql server 2008 on Server 2003 than your server  should have service pack 2 install.if it’s not then download service pack 2 from the following link, install it ,reboot your machine and then start installing sql server 2008.

now,if you all set for installing environment,you can proceed for the following steps

Installation Steps:-

Step 1:

Start installation of sql server 2008 by double clicking on  the “setup.exe”

SQLServer2008Install_01_Files 

Step 2:

If the prerequisites installation dialog box appears, click OK

SQLServer2008Install_02_PrerequisitesPopUp

Step 3:

If you machine don’t have .NET Framework 3.5 SP1 .It will give a dialog box ,accept the License Agreement and it will start installing it.after it complete installation Exit button will appear in the right  bottom corner of the dialog box  with setup completed message click on that.to Proceed  further.

SQLServer2008Install_03_Framework35SP1

SQLServer2008Install_04_Framework35SP1Install

SQLServer2008Install_05_Framework35SP1InstallSuccesfull

Step 4:

After  installing the NET Framework 3.5 SP1 it may ask you to install the Hot Fix for xp/windows server 2003.Accept the License Agreement to  Install it.after successfully installation it may give  prompted to you for restarting  your computer, restart it, and then restart SQL Server 2008 setup.exe.

SQLServer2008Install_06_WinInstaller45Prompt

SQLServer2008Install_07_WinInstaller45License

SQLServer2008Install_08_ServiceInterruption

SQLServer2008Install_09_WinInstaller45Finish

SQLServer2008Install_10_RestartPrompt

Step 5:

When the prerequisites are installed, the Installation Wizard will run the SQL Server Installation Center. To installation of SQL Server 2008, click New Installation or Add Features to an Existing Installation.

SQLServer2008Install_11_NewInstallation

Step 6:

It will  check  that you system is configure proper for installing the sql server 2008.if any Error occur during this test need to resolve that error and then only you able to Proceed  further.Click ok when you pass all the check.

SQLServer2008Install_12_SetUpSupportRules

Step 7:

If you able to proceed  further then,in next dialog box will ask you to either install free version or the product key.go one of the way to install.

SQLServer2008Install_13_ProductKeyPage 

Step 8:

Next step is to accept the License Agreement  and click ok.

SQLServer2008Install_14_LicenseTermsPage

Step 9:

The Installation Wizard will install SQL Server prerequisites if they are not already on the computer. These include the following:

  • .NET Framework 3.5 SP1
  • SQL Server Native Client
  • SQL Server Setup Support Files

To install prerequisites, click Install.

SQLServer2008Install_15_SqlServerPrerequisites

Step10: 

Select  the Feature form the Feature  Selection you want to install.

SQLServer2008Install_17_SelectingInstanceFeatures

Here is the  some more information on the current choices:

  • Data Base Engine is obviously mandatory
  • Business Intelligence Development Studio aka BIDS
    This is essential regarding Reporting Services integration as it is THE tool that will allow you to create reports and deploy them in SharePoint. For a SQL Server 2008 installation without Reporting Services you can avoid installing this feature.
  • Management-Tools Complete
    This feature will install Microsoft SQL Server Management Studio
    If you don’t need Reporting Services, you can avoid checking complete feature, but the complete one install elements that runs with Reporting Services as noticed in description that appears in the right pane after you select the feature name.

You can also specify a custom directory for shared components by using the field at the bottom of the Feature Selection page. To change the installation path for shared components, either update the path in the field at the bottom of the dialog box, or click Browse to move to an installation directory. The default installation path is C:\Program Files\Microsoft SQL Server\100\.

Step 11:

On the Instance Configuration page, specify whether to install a default instance or a named instance.

SQLServer2008Install_18_InstanceConfiguration

Step 12:

The Disk Space Requirements page calculates the required disk space for the features that you specify. Then it compares the required space to the available disk space.

SQLServer2008Install_19_DiskSpaceRequirements

Step 13:

On the Server Configuration — Service Accounts page, specify login accounts for SQL Server services. give your system Administrator account and specify the password so it has all the access.

SQLServer2008Install_20_Accounts

Step 14:

Chose Mixed mode if you want to integrate you sql with share point to use SQL Authentication. you need to provide the password for the “sa” login if you have selected mixed  mode.then Click on the Add current User.

SQLServer2008Install_22_AuthenticationMode-Admins

Step 15:

if you planning to install the Reporting service.Reporting service configuration screen will com up.

select first option you you want to configure Reporting service automatically as installation process.

select second option if you have already install share point and want to configure Reporting service with sharepoint  automatically as installation process.

Select third option you want to configure reporting service manually or you have not install the share point yet.

SQLServer2008Install_23_ReportingServicesConfig

Step 16:

On the Error and Usage Reporting page, specify the information that you want to send to Microsoft that will help improve SQL Server. By default, options for error reporting and feature usage are enabled.

SQLServer2008Install_24_ErrorandUsReporting

Step 17:

now one more time System configuration check will run to check that all the option will you selected are able to install in you current configuration.

in case,it fail to pass any of the one check ,need to resolve it to proceed further.

SQLServer2008Install_27_InstallationProgress

Step 18:

The Ready to Install page shows a tree view of installation options that were specified during Setup. To continue, click on Install will start the installing the sql server 2008.

SQLServer2008Install_26_ReadyToInstall

 

Next Post:

Configuration of the Reporting service and integrating the reporting service with share point will going to come on my next post.

Enjoy you successful installation of SQl server 2008.

Access is denied. (Exception from HRESULT: 0×80070005 (E_ACCESSDENIED))

NOTE :-  This post is only  for the Reporting service 2008.

access denide ssrs2008

Solution:-

To resolve this issue go through following Steps:-

(1) Go to the Internet Explorer.

(2)Right Click on Internet Explorer.

(3)Click on the Run as.. optional in the menu. Run As Popup will open.

(4)In the popup select The following user option. And give Administrator as Username And give password in password text.and click ok.

Now,Go to your Reporting Server Configuration Manager ,and click on the webserviceUrl link or Report server Manager Link ,you will able to view the page.

Enjoy your reporting service!!!!!

Textbox readonly in asp.net

If TextBox’s ReadOnly property is “true”, postback data won’t be loaded e.g it essentially means TextBox being readonly from server-side standpoint (client-side changes will be ignored).

If you want TB to be readonly in the “old manner” use

TextBox1.Attributes.Add(”readonly”,”readonly”)

as that won’t affect server-side functionality.