Thursday, July 14, 2011

SharePoint Feature

SharePoint allows developers to create solutions and deploy to its platform. One way to deploy a solution and manage it is using feature. Feature is a combination of several elements that can be turned on or off at various levels or scope.

  • Farm scope (Farm)

image

  • Web application scope (WebApplication)

image

  • Site Collection scope (Site )

image

  • Web site  scope (Web)

image

 

Feature is physically a xml file named feature.xml. Feature is accompanied by an elements file, commonly named elements.xml. This file name can be anything as long as it is reflected properly in feature.xml file. The feature.xml file specifies name of the feature and its scope as well as the name of the elements file.  Elements file can have custom actions and modules that are part of this feature to be deployed. A feature can also receive events such as feature activated or deactivated.

Following are the steps to create a feature and deploy it manually -

  1. Create a class library project
  2. Create appropriate folder structure, for example
    • Templates
      • Feature
        • CustomFeature
  3. Add two files, example below
    1. Feature.xml
         1: <Feature 



         2:   Id="EEC24C3D-B14A-4c4b-8673-5E196E7DDA17"



         3:   Title="My Custom Feature"



         4:   Description=""



         5:   Scope="Web"



         6:   Hidden="false"



         7:   ImageUrl="TPG\binoculars.gif"  



         8:   xmlns="http://schemas.microsoft.com/sharepoint/">



         9:   <ElementManifests>



        10:     <ElementManifest Location="elements.xml"/>



        11:   </ElementManifests>  



        12: </Feature>





    2. Elements.xml


         1: <Elements xmlns="http://schemas.microsoft.com/sharepoint/">



         2:   <Module Path="PageTemplates" Url="SitePages" >



         3:     <File Url="MyPage.aspx" Type="Ghostable" />    



         4:   </Module>  



         5: </Elements>







  4. Add a aspx page mypage.aspx and modify as you wish.


  5. Sign the project and build


  6. deploy the dll to GAC


  7. deploy the feature using stsadm


       1: stsadm.exe -o installfeature -filename customfeature\feature.xml -force




Monday, July 11, 2011

An Oracle Primer for SQL Server Developers

Recently I had the opportunity to get my hands on Oracle. Coming from a SQL server b background it certainly seemed strange at first but got easier over time. The biggest challenge was to find resources that use Oracle to SQL server comparison to teach the material. So I decided a write this one to help out anybody who is interested.

Oracle Database

Oracle database consists of an instance and database files. An Oracle Instance is the process in memory that manages the database files. As opposed to SQL server, Oracle does not have a concept of individual databases. Every user in Oracle owns a schema and database objects such as tables are created under the schema. Schema owners own all objects on their schema. Different types of permissions are assigned to users that do not own the objects if desired.

Connecting to Oracle using SQL Developer

SQL Developer is one of the free and more popular Oracle management tool. There are various ways to connect to an Oracle database server using SQL Developer–

clip_image002

1. Basic – With basic, user will need to provide following fields –

a. HostName

b. Port

c. UserName

d. Password

2. TNS – One only needs to point to the Network alias. The list of Network Alias comes from a .ora file. Sql Developer looks for tnsnames.ora file in following locations -

a. $HOME/.tnsnames.ora

b. $TNS_ADMIN/tnsnames.ora

c. TNS_NAMES lookup key in the registry

d. /etc/tnsnames.ora ( non-windows )

e. $ORACLE_HOME/network/admin/tnsnames.ora

f.LocalMachine\SOFTWARE\ORACLE\ORACLE_HOME_KEY

g. LocalMachine\SOFTWARE\ORACLE\ORACLE_HOME

You can add a environment variable entry called TNS_ADMIN with folder location to the tnsnames.ora file. A typical entry looks like following –

   1: dev = 
   2: (DESCRIPTION = 
   3: (ADDRESS = (PROTOCOL = TCP)(HOST = 
   4: 192.168.XXX.XXX)(PORT = XXXX)) 
   5: (CONNECT_DATA = 
   6: (SERVER = DEDICATED) 
   7:  
   8: (SERVICE_NAME = idpdev2) 
   9: ) 
  10: )

As you can see, the connection string is a little more detailed in the ora file.


3. LDAP – Oracle connection information can be stored in a directory that implements LDAP directory services. If Oracle Internet Directory is setup on the network, one can use its host name and browse to a DB Service from the list of DB services.


Connecting to SQL Server from SQL Developer


SQL Developer cannot connect to SQL server out of the box is some configuration is necessary. Follow this steps to connect to a SQL Server instance –



  1. Enable TCP/IP from SQL server configuration manager
  2. Look for current port used in SQL server configuration manager in TCP/IP nodeclip_image004
  3. In this scenario, Dynamic port is set to 50270. You can clear it and set TCP Port so SQL server always listens to this port. Otherwise, from SQL developer you will need to use 50270 as port number.
  4. Download JTDS from http://sourceforge.net/projects/jtds/files/jtds/
  5. Extract jtds and copy ntlmauth.dll from \x86\SSO or x64\SSO folder to \sqldeveloper\jdk\jre folder
  6. Start SQL Developer and go to Tools -> Preferences -> Database -> Third Party JDBC Drivers
  7. Click Add Entry and point to jtds-[version].jar file. The Add Entry button allows to add directory as well so be careful to the file instead of the directory it is in.
  8. Click Ok and restart SQL Developer
  9. Click on Add connection
  10. You will see a new tab beside Oracle and Access.
  11. Type in your host name and port and click retrieve database. Note, I connected to SQL Express .\sqlexpress instance, but in Host I had to enter localhost (without the instance name).
  12. You will see a connection on the left pane as below –


clip_image006


 


Hello World in Oracle


Following is an example of Hello World –



   1: set serveroutput on;
   2: begin
   3: DBMS_OUTPUT.PUT_LINE('Hello World');
   4: end;

You will notice that unlike SQL Server, Oracle statements must run in an anonymous block (BEGIN/END). In addition, server output is set to on so output appears in Script Output window.


Roles and Privileges


Privileges are the rights to perform some task in the Oracle system. Roles are groups of privileges and roles.


Role: Oracle comes with predefined roles. Users can create more roles. Here are some examples of some of the predefined roles –



  1. Connect – can create session
  2. Resource – create table, type, trigger
  3. DBA – all system privileges

Following tables hold roles and their mapping to users –



  1. DBA_ROLES – All roles
  2. DBA_ROLE_PRIVS – All roles mapped to users

Privileges: There are two kinds of privileges –



  • System privileges: Create session, tables, procedures
  • Object privileges: Select/Insert/Delete in Tables/Views etc. Following is an example of grant on hr.candidates package to user amyo. The with grant option setting also allows amyo to grant execute to other users.


   1: grant execute on hr.candidates to amyo with grant option

Stored Procedures and Packages


Oracle can have stand-alone stored procedures just like SQL Servers. In addition, Oracle has packages. Packages are similar to C++ classes where users declare and define private or public data types and methods. The syntax to define a procedure is similar to SQL Server. However, Oracle does not support following in procedures-



   1: SELECT col1, col2 from table_name

You will need to declare a cursor and select into the cursor to return data as following –



   1: PROCEDURE MY_PROCUDURE(p_cursor OUT sys_refcursor) AS 
   2: BEGIN
   3: OPEN p_cursor FOR
   4: SELECT col1, col2 FROM table_name
   5: END MY_PROCDURE;

An Oracle package has a specification and body. A package can have types, variables and procedures.


· Package with type in it –



   1: CREATE PACKAGE trans_data AS
   2: TYPE TimeRec IS RECORD (
   3: minutes SMALLINT,
   4: hours SMALLINT);
   5: TYPE TransRec IS RECORD (
   6: category VARCHAR2(10),
   7: account INT,
   8: amount REAL,
   9: time_of TimeRec);
  10: minimum_balance CONSTANT REAL := 10.00;
  11: number_processed INT;
  12: insufficient_funds EXCEPTION;
  13: END trans_data;


· Package that contains a procedure –


Spec –



   1: CREATE OR REPLACE
   2: PACKAGE CANDIDATES AS 
   3: PROCEDURE GET_ALL_CANDIDATES(p_cursor OUT SYS_REFCURSOR);
   4: END CANDIDATES;

Body –



   1: create or replace PACKAGE BODY CANDIDATES AS 
   2: PROCEDURE GET_ALL_CANDIDATES(p_cursor OUT SYS_REFCURSOR) AS 
   3: BEGIN
   4: OPEN p_cursor FOR
   5: SELECT * FROM PERSON WHERE IS_EMPLOYEE = 0;
   6: END;
   7: END CANDIDATES;

To execute the package from SQL Developer using code -



   1: declare p_cursor sys_refcursor;
   2: person_id number;
   3: full_name VARCHAR2(100 BYTE);
   4: begin
   5: candidates.get_all_candidates(p_cursor) ;
   6: LOOP 
   7: EXIT WHEN p_cursor%NOTFOUND; 
   8: FETCH p_cursor INTO person_id, full_name; 
   9: DBMS_OUTPUT.PUT_LINE(person_id || ',' || full_name); 
  10: END LOOP;

To execute a package visually –



  1. Right click on package name from Connection pane
  2. The Run PL/SQL windows shows
  3. Select Target procedure update any parameters with value and click ok.


clip_image008



4.  The result will show in Output variables window



clip_image010


 



Oracle and SQL Server data type comparison


Please see the comparison table to see the data type mapping between the two -


http://msdn.microsoft.com/en-us/library/ms151817.aspx


Oracle Synonym


Oracle allows to create names for many types of objects including Table, View, Procedures, functions and other synonyms. Synonyms can be private or public. Private synonyms are accessible to Oracle users who own the synonym and public synonyms are accessible to all users.



   1: CREATE SYNONYM offices 
   2: FOR hr.locations;CREATE PUBLIC SYNONYM emp_table  FOR hr.employees@remote.us.oracle.com;
  

Monday, May 24, 2010

Sql Server 2008 User Instances

Sql Server Express edition packs something really cool – User Instances.

Let me elaborate. I have following requirements -

  1. A windows application
  2. A single user database
  3. Click-once deployment

I found out I can create a solution with Sql server User Instance and  local database really easily. This is one way to do this -

Add Database to Visual Studio

Create a windows forms application solution in Visual Studio.  Next create the database using SSMS. Detach the database and copy/add the mdf file to visual studio solution. Alternatively, you can add a local database in Visual Studio and edit the database model from VS.

Update Project

Update project properties by right clicking on project and selecting properties. Verify that in publish tab\Application files your mdf file is setup as data file and required. Also make sure, mdf file Build Action is set to Content and Copy.

Since we will use Sql Server Express 2008, you will need to install Visual Studio 2008 Express edition.  This is because Visual Studio 2008 Standard came up before Sql Server 2008 and does not have the necessary Sql Server 2008 bootstrapper. When you install VS Express, make sure you select Sql Server Express to install as well.

Add Sql Server 2008 Express edition to your list of pre-requisites under Publish tab.

You may also add a update url if you choose.

Setup Connection

Create a database connection string in your config file as following -

<add name="MyConnectionString"  connectionString="Server=.\SQLExpress;AttachDBFileName=|DataDirectory|\mydatabase.mdf;Database=mydatabase;Trusted_Connection=Yes;User Instance=True "
      providerName="System.Data.SqlClient"/>

During click-once deployment, Installer will copy the mdf file to the data folder in your profile directory (directory address varies among flavors of windows – in Windows 7 it is here: C:\Users\<username>\AppData\Local\Apps\2.0\Data). During update, the mdf file will be left alone. You will need code to update your database.

Publish

Publishing itself is straightforward. Choose your deployment option (Web/CDROM etc) and hit deploy. When user runs the installer, Sql Server Express will auto-download and install. The database file will attach itself and stay attached. No hassle on your part!

Few things to remember:

You won’t be able to view your database if you connect to the Express instance. this is because, when you specify User Instance = true in your connection string, Sql Server runs a special instance of Sql Server that allows non-admin users to create/attach database. For troubleshooting use following Sql command to find your instance –

SELECT owning_principal_name, instance_pipe_name, heart_beat FROM sys.dm_os_child_instances

You can use the value for instance_pipe_name as your server name to connect to this instance. this instance will shut down after 60 minutes of inactivity.

Also, if you want to have the option to allow user to be able to use either Standard Sql Server or Express, you can modify the connection string at runtime. Just remove the “User Instance”  key.

 

     

Wednesday, April 21, 2010

Virtual PC Annoyances

I use Virtual PC exclusively for development as well as testing. There has been many times I have run into issues that were not show stoppers but the fix were not obvious. In this Blog I will add the problems and solutions to do with VPC.

  • Problem: No network connection when using Virtual PC.
  • Resolution: Check following items
    1. Host has network connection
    2. In VPC settings, correct nic is selected
    3. Restart the VPC
    4. Remove nic, restart, add nic
    5. Shutdown VPC. Delete Options.xml file from %appdata%\Microsoft\Virtual PC. Start VPC

Managing Application Settings with version changes

One can easily use ApplicationSettingsBase class to manage application settings on user or application level. When used with user scoped settings, configuration values will be lost with version upgrades. We can use following code at application launch to update configuration values from old configuration file.


String strVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

if (Properties.Settings.Default.CurrentVersion != strVersion) {
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.CurrentVersion = strVersion;
}

Thursday, April 1, 2010

Use self signed certificate for self hosted WCF service

Recently I was looking to see how I could configure a WCF service to use SSL. I came across this article (SSL with Self-hosted WCF Service) that states the steps nicely.

In case this article is no longer available; I decided to record the steps -

There are few steps involved in this.

  1. One must be a Certificate Authority to issue certificates. For self signed certificates you can make yourself a CA by issueing following command –

    makecert -sv SignRoot.pvk -cy authority -r signroot.cer -a sha1 -n "CN=Dev Certification Authority" -ss my -sr localmachine

    Here you become a CA called "Dev Certification Authority" and set your local machine as CA server.

  2. Create a certificate for the server that will host your service (such as WCF, web service etc). Issue following command for this –

    makecert -iv SignRoot.pvk -ic signroot.cer -cy end -pe -n
    CN="localhost" -eku 1.3.6.1.5.5.7.3.1 -ss my -sr
    localmachine -sky exchange -sp
    "Microsoft RSA SChannel Cryptographic Provider" -sy 12

  3. You will use this certificate to setup SSL . Open the certificate you created in step 2. It is in Certificate MMC\Personals folder. Now go to details tab. You will see thumbprint all the way down. Copy it somewhere and remove all spaces.

  4. Next issue the httpcfg command to connect it to your desired port(8012 in this case) –

    httpcfg set ssl -i 0.0.0.0:8012 -h copiedthumbprintfromstep2

  5. There are additional httpcfg command that you can use to query and delete SSL ports as well -
    • httpcfg delete ssl -i 0.0.0.0:8888
    • httpcfg query ssl

  6. On target machine install the root certificate by opening certificates MMC and do Import. Make sure you pick Physical store and pick Computer as the store. Otherwise, it will not appear in certificates.