Monday, October 6, 2014

Different values of SPSite.URL and SPWeb.URL 

------------------------------------------------------------------------------------------------------------
There are some properties for SPSite and SPWeb class using which we can get the URL values for the sites and webs. The two most used of these properties are - 'ServerRelativeUrl' and 'Url'
As a developer we use the values of the properties for writing some logic for satifying the business needs. Whenever I used to write a OM code to use these properties, the first question which flashes in my mind - "What values are returned by these properties?". I immediately create a cosole application, and check the values returned by these properties and contiue with the actual work. I have seen many other developers doing this. So, for a quick reference I prepared the below table which has the url values returned by these properties.
Some poeple may find this easy, but some (like me ;)) get confused when the sites have managed paths. These properties return the managed path if the site has one. Here goes the list
SCENARIO 1: A Site Collection (SPSite) without any managed path.
URL : 'http://rams'
PropertyValue
SPSite.ServerRelativeUrl/
SPSite.Urlhttp://rams
SPWeb.ServerRelativeUrl/
SPWeb.Urlhttp://rams
Url - http://rams/about ('about' is a sub site)
SPSite.ServerRelativeUrl/
SPSite.Urlhttp://rams
SPWeb.ServerRelativeUrl/about
SPWeb.Urlhttp://rams/about
This looks pretty simple. We get confused when sites are created using managed paths. Below are the samples values for site created with managed paths
SCENARIO 2: A Site Collection with a managed path (of type Explict inclusion). For the below URL '/my' is the managed path
URL : 'http://rams/my'
PropertyValue
SPSite.ServerRelativeUrl/my
SPSite.Urlhttp://rams/my
SPWeb.ServerRelativeUrl/my
SPWeb.Urlhttp://rams/my
Url - http://rams/my/about ('about' is a sub site)
SPSite.ServerRelativeUrl/my
SPSite.Urlhttp://rams/my
SPWeb.ServerRelativeUrl/my/about
SPWeb.Urlhttp://rams/my/about

SCENARIO 3: A Site Collection with a managed path (of type Wilcard inclusion). For the below URL '/sites' is the managed path, and a site collection is created at '/blog'
URL : 'http://rams/sites/blog'
PropertyValue
SPSite.ServerRelativeUrl/sites/blog
SPSite.Urlhttp://rams/sites/blog
SPWeb.ServerRelativeUrl/sites/blog
SPWeb.Urlhttp://rams/sites/blog
Url - http://rams/sites/blog/about ('about' is a sub site)
SPSite.ServerRelativeUrl/sites/blog
SPSite.Urlhttp://rams/sites/blog
SPWeb.ServerRelativeUrl/sites/blog/about
SPWeb.Urlhttp://rams/sites/blog/about

Wednesday, April 23, 2014

Exporting deployed WSP's of your SP Farm

If you want to get wsp out of deployed WSP's in your fram, execute below power shell code.

Copy below lines of code to a power shell file. i.e., file with extention "ps1" and go to windows powershell command or SharePonit's and run the prepared file. 

Tuesday, April 15, 2014

Adding JavaScript/Jquery code in InfoPath forms

Some times you may face requirement to access controls of InfoPath form in javascript. Then below approaches may helpful.

Example requirement: Client asks you to handle enter key to navigate from one control to another control on the form or execute rules when user enters a value in a text box and hits enter. Usually, in infopath form, we use Tab key instead Enter key for such requirements.

To fulfill such requirements, we have to go with JavaScript code that will read the enter key number (13) and then you will make that as Tab key(if i am not wrong, i guess it is 8).

There are two ways in adding JavaScript for InfoPath forms.

Method 1) in code behind of InfoPath form
Method 2) in FormServer.aspx page in layouts folder

Method 1 will effect the script funcionality only on your form. where as method 2 will effect for all forms. because InfoPath forms will load as control on FormServer.aspx page.

In Method 2 also, you have to use delay() method in javascript before reading the controls. Because, generally the script will execute before the control i.e., form loads. writing delay(1000) will help the script wait for 1 second and in this time, the form will load and so you can read the controls based on your requirement.


For method 1, you need to write in the code as shown in below example:

//This is to handle back space key press.
 //page should not go back when user clicks backspace key. It will work as usuall when cursor is in any control
                HttpContext.Current.Response.Write("<script type='text/javascript'>" +
                    "if (typeof window.event != 'undefined')" +
                    "{document.onkeydown = function(){" +
                       "if (event.srcElement.tagName.toUpperCase() != 'INPUT')return (event.keyCode != 8);}}" +
                       "else{document.onkeypress = function(e){if (e.target.nodeName.toUpperCase() != 'INPUT')return (e.keyCode != 8);}" +
                       "}</script>");



happy coding :)



Debugging SharePoint's custom timer jobs from visual studio

I guess this topic is there in many sites. but still want to update in my blog.

when you want to debug a custom sharepoint timer job from visual studio 2008 or above, follow below steps:

1. After you deploy your timer job, you need to restart SharePoint timer service that will be available in list of windows services on your server machine.
2. Now go to debug and attach to process and select OWSTIMER process. If you are not finding it, click refresh button multiple times and check. After attaching,
3. Keep a break point in execute method.
4. go to central admin-->monitoring and find your deployed timer job and Run It using "run now" button

that's all, if you did all correct, you will see the execution will take you to the break point that you placed in your execute method.


happy codding :)
Accessing List or Library in Custom SharePoint TimerJob code of a site without Hard-codding site URL/ID

When we write any custom SharePoint timer job using visual studio, some times, we may need to access values from a list / library. and in such cases, some times you may not be predict the site ID/URL in which the list / library present.

In such scenario's the best options is to develop a feature with "site" scope. and use this feature to create timer job instance and also to get required list object. This feature GUID will be used for getting site collection where your list/library exists.

Once after creating feature, note down the GUID and now go to execute method of your timer job. In that method, you get the webapplication object and iterate through all its site collection. Now find the site collection in which the above feature with GUID is activated. Take that site collection as your SPSite object and access your required lists.

In this way, you can avoid hard coding site URL or ID. only thing you have to make sure is, this feature should be activated only in your required site collection.


Example: You have to write a timer job that will execute every week on sunday. And this job should read items from a master list called "EmployeesToBeNotified" which contains all employees to whom you have to send notifications using email.

Sample Code:

 public override void Execute(Guid targetInstanceId)
        {
               SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    System.Diagnostics.EventLog.WriteEntry("TimeSheet TimerJob", "TimeSheet TimerJob Executed");
                     SPWebApplication webAppNew = (SPWebApplication)this.Parent;
                     if (webAppNew.Sites.Count > 0)
                     {
                         Guid NewSiteID =new Guid();
                         foreach (SPSite timesheetSite in webAppNew.Sites)
                         {
                             Guid featureGuid = new Guid("d5ef5623-270a-4c8c-b4c5-784b82306b87");
                             if (timesheetSite.Features[featureGuid] != null)
                             {
                                 NewSiteID = timesheetSite.ID;
                                 timesheetSite.Dispose();
                                 break;
                             }
                       
                         } //close of foreach

                             using (SPSite site = new SPSite(NewSiteID))
                               {
                                    //write your required code here.
                                }

                     } //close of outer if
                 });       //close of spsecurity
     } //close of execute method

Send email with CC and BCC using SPUtility in SharePoint

When i am searching for options to send CC/BCC using SharePoint's SPUtility class, i came across this code. Thought of sharing in my blog which might be helpful for any one



StringDictionary headers = new StringDictionary();
headers.Add(“to”, sendTo);
headers.Add(“cc”, “”);
headers.Add(“bcc”, “”);
headers.Add(“from”, “admin@test.com“);
headers.Add(“subject”, “Testing”);
headers.Add(“content-type”, “text/html”);
SPUtility.SendEmail(web,headers, Body);



share your comments/feedback if any.

thank you :)