Tuesday, 26 March 2013

Adding details to database table using SharePoint List event receiver

Here's a sample code to give you idea about how to add item details to database table using SQL stored procedure and ItemAdded event receiver,




    class myClass : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            try
            {
                this.DisableEventFiring();

                AddToDataBase(properties);
            }
            catch (Exception ex)
            {
                //log here
            }
            finally
            {
                this.EnableEventFiring();
            }
        }

private void AddEntryToDataBase(SPItemEventProperties properties)
        {
using (SqlConnection con = new SqlConnection(dc.Con))
using (SqlCommand cmd = new SqlCommand("sp_Add_ListItem", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = properties.ListItem.Title.ToStrinng;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = properties.ListItem.Name.ToString;

con.Open();
cmd.ExecuteNonQuery();
}
}
}

Check this link on MSDN for more information or just leave a comment below,

http://msdn.microsoft.com/en-us/library/ms437502(v=office.14).aspx

Wednesday, 20 March 2013

Random STSADM commands

I will be adding random STSADM commands for future references,


Getting deployment jobs 
stsadm -o enumdeployments


Cancelling a deployment job
stsadm -o canceldeployment -id



Executing deployment jobs
stsadm -o execadmsvcjobs

Tuesday, 19 March 2013

Web Method of custom SharePoint 2007 web services not getting update


Web Method of custom SharePoint 2007 web services not getting update

I created custom web services for SharePoint 2007 which I am going to consume in K2 2003 Server work flow. I added a new web method to custom web services (How to create custom web services) and then to make it work in my SharePoint 2007 environment I followed following steps,


  • Created a new virtual directory for web services in IIS Manager web services application with application pool that has been used for services.
  • When you are updating or adding something new to your web services solution, you will have to delete bin folder of the solution and re-compile solution in both debug and release mode or as required.

  • Point virtual directory to your web services solution, mine is on desktop as I am developing and debugging instead of developing, building and then putting files in virtual directory and I think most developers do same.
  • Run disco Commands in Visual Studio 2008 Command Prompt e.g.

    C:\Program Files\Microsoft Visual Studio 9.0\VC>disco http://localhost:99999/mySharePointLists2.asmx
  • Above command will get you two files in same folder directory as above.

In both files, replace the XML instruction:


<?xml version="1.0" encoding="utf-8"?>



With



<%@ Page Language="C#" Inherits="System.Web.UI.Page" %>

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Import Namespace="Microsoft.SharePoint.Utilities" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<% Response.ContentType = "text/xml"; %>


In the .disco file, replace the <contractRef> tag with one using virtualised URLs:



<contractRef ref="http://localhost/web/customWebservice.asmx?wsdl" docRef="http://localhost/web/customWebservice.asmx" xmlns="http://schemas.xmlsoap.org/disco/scl/" />



With



<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request) + "?wsdl"),Response.ContentType = "text/xml"; %>
And update the <soap address> tags to use virtualised URLs and the correct method bindings and XML namespace:



<soap address="http://localhost/web/customWebservice.asmx" xmlns:q1="http://dev.com/" binding="q1:CustomWebserviceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

<soap address="http://localhost/web/customWebservice.asmx" xmlns:q2="http://dev.com/" binding="q2:CustomWebserviceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />



With



<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q1="http://tempuri.org" binding="q1:CustomWebserviceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q2="http://tempuri.org" binding="q2:CustomWebserviceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />




In the .wsdl file, replace the <soap address> tags with virtualised versions:



<wsdl:service name="CustomWebservice">

<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Web Service Description here...</wsdl:documentation>

<wsdl:port name="CustomWebserviceSoap" binding="tns:CustomWebserviceSoap">

<soap:address location="http://localhost/web/customWebservice.asmx" />

</wsdl:port>

<wsdl:port name="CustomWebserviceSoap12" binding="tns:CustomWebserviceSoap12">

<soap12:address location="http://localhost/web/customWebservice.asmx" />

</wsdl:port>

</wsdl:service>



With



<wsdl:service name="CustomWebservice">

<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Web Service Description here...</wsdl:documentation>

<wsdl:port name="CustomWebserviceSoap" binding="tns:CustomWebserviceSoap">

<soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />

</wsdl:port>

<wsdl:port name="CustomWebserviceSoap12" binding="tns:CustomWebserviceSoap12">

<soap12:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />

</wsdl:port>

</wsdl:service>


Rename the two files to have an .aspx extension, e.g. customWebservicedisco.aspx and customWebservicewsdl.aspx
Deploy the .asmx, disco and wsdl files to 12\ISAPI. Check the NTLM permissions!
Deploy the DLL to the GAC and to the web applications /bin directory (check the NTLM permissions again). DO NOT know why both are necessary… Probably a CAS thing, you could probably get the same result by installing to the \bin directory only and raising the trust level on the server to full; or by creating a custom CAS policy
Reset IIS
Should be able to access the web service at http://portalsite.com/_vti_bin/webservice.asmx

Tuesday, 5 March 2013

Random SQL Queries statements

I will be adding scripts or queries for future references that I sometimes have to use while working on SQL Server Management to interact with database,


Adding a new column to database table and populating value for records that are already there

Alter Table Table_Name Add Column_Name ColumnType

Go

Update Table_Name Column_Name = Value

Disabling controls within a CSS class

I am working on a task where I needed to disable all of controls within a class. I have tried several ways but only one worked for me which is,

$(".myCSSClassName:input").attr('disabled', true);

This will definitely work when you have a div with class named as "myCSSClassName:input" it will disable all controls within this specific class.

Other possible ways of disabling controls within a CSS class are mentioned below but they didn't worked for me,


$('input.myClass').click(function(e){
    e.preventDefault();
})


$('input.myClass').attr('disabled', '');

$("input.myClass").prop("disabled", true);

$('input.myClass').attr('disabled', 'disabled');


There are few other options that seems promising can view on this link.

Tuesday, 12 February 2013

How to use ULS Viewer in SharePoint environment

ULS Viewer a great tool that can be used by a SharePoint developer or Administrators to debug and find errors within a SharePoint environment. I personally use this tool when I am badly stuck somewhere while configuring or deploying a new feature and event receiver don't catch any meaningful error.

You can run this tool and try to do something with SharePoint, it will display logs for what's happening behind the scene. Here's a small guide on how to use ULS Viewer on a SharePoint server.

Download ULS Viewer using link given below,

Download ULS Viewer

Keep it "ULS Viewer.exe" somewhere you can easily access it, e.g. on Desktop.

Now Click on "File" > "Open From" > "ULS" or Simply press "Ctrl + U" as shown in picture below,




Another window will appear which will be pre-populated by directory for your ULS logs on SharePoint server, as shown in picture below,




Just click on "OK" and you will "Real Time" logs of what's happening behind the scenes in your SharePoint environment. By default it shows you all types of messages but you can only select "Show High Messages" which will trim down logs to display you errors with high priority.

Friday, 1 February 2013

Execute Timer Jobs in SharePoint 2007

Sometimes I want my custom SharePoint solutions to execute faster then normal or when they stuck, I use this STSADM command to execute timer jobs that are in waiting list,




stsadm -o execadmsvcjobs



However it will not trigger any custom timer job you deployed in your SharePoint 2007 Farm.