Wednesday, July 20, 2011

Using SharePoint 2010 Processing Page in code

For long operations, one could use the following code in order to display the SharePoint's default processing page.

string

{
      operation.Begin();

      //Your Code

      operation.End(comeBackUrl, Microsoft.SharePoint.Utilities.
}
SPRedirectFlags.DoNotEncodeUrl, System.Web.HttpContext.Current, null);

comeBackUrl = SPContext.Current.Web.Url.ToString();using (SPLongOperation operation = new SPLongOperation(this.Page))

Error: Web site worker process has been terminated by IIS

Error Message: The web server process that was being debugged has been terminated by Internet Information Services (IIS), This can be avoided by configuring Application Pool ping settings in IIS

Solution:
In Internet Information Services (IIS) Manager -> Connections pane -> Application Pools -> Advanced Settings -> Process Model section, do one of this:

Set Ping Enabled to False.

Set Ping Maximum Response Time to a value that is larger than 90 seconds.

Monday, July 18, 2011

Activate a feature programmatically

SPFarmFeatureCollection keeps a list of all activated features on a site or site collection.  Those can be accessed using the Features property of SPWeb or SPSite objects.

WebFeatures or SiteFeatures property of SPContext.Current can also be used.

Add() and Remove() methods with the GUID as the arguement would do the work.  One thing to keep in mind here is that if we try to activate an already activated feature using Add(), an InvalidOperationException is thrown.  The only way to check if it is already activated or not is by using indexer and see if an exception is thrown.

SPWeb spCurrentSite  = SPContext.Current.Web;

spCurrentSite.Features.Add(new GUID("{G__U__I__D}"));
(or)
spCurrentSite.Features.Remove(new GUID("{G__U__I__D}"));


In case the GUID is not known, then we would have to chek out for the name in the FeaturesDefinitions collection of the SPFarm Object.

Saturday, July 16, 2011

2nd Interview Experience

Not much this time around.  The role was of a SharePoint Admin.


  1. New features added to SP2010
  2. Difference between BCS and BDCBusiness Connectivity Services (BCS) is the new name for the Busienss Data Catalog technologies from SharePoint 2007. BCS is enhanced in the 2010 release with read/write capabilities, support for Windows Communication Foundation (WCF), and new client capabilities so that you have APIs both on the server and client and can sync Line-of-Business (LOB) data from your backend systems to the client cache and work on that LOB data when offline. BCS will synchronize that data from the client with server when you can reconnect.
  3. Difference between stsadm and PowerShell (Answer is here)  In a nutshell, We recommend that you use Windows PowerShell when performing command-line administrative tasks. The Stsadm command-line tool has been deprecated, but is included to support compatibility with previous product versions.
More questions were on my experience of working on admin project.  

Friday, July 15, 2011

Understanding WorkFlows

2 Types:

  1. Sequential: has predictable paths of execution
  2. State Machine: has many possible ways of execution as it includes conditions such as While, IfElse, Replicator etc.
When we create custom workflows, we get 5 files in our project:
  1. feature.xml - Contains the definition of the feature that will eventually deploy the WF in the server
  2. workflow.xml - defines the WF
  3. install.bat - contains command line instructions to copy the files to TEMPLATE folder and has stsadm commands to install the WF.
  4. workflow.cs - inherits the base class SequentialWorkflowActivity
  5. workflow.designer.cs


Ways of Deploying WebParts

Manual Way



  1. Copy the assembly DLL to either the bin or to the GAC (needs to strong named)
  2. Copy the DWP File to c:\inetpub\wwwroot\wpcatalog
  3. Copy resource files to C:\Program Files\Common Files\Microsoft Shared\web server extensions\wpresources for GAC registered Webparts and to C:\Inetpub\wwwroot\wpresources for webparts that are copied to the bin folder
  4. Add SafeControl entries to the web.config

Life Cycle of a WebPart

1. OnInit - Handles the initialization of the webpart

2. OnLoad - Handles the load event

3. CreateChildControls - Creates all the child controls if present

4. EnsureChildControls - Ensures that the CreateChildControls method has been executed

5. OnPreRender - Handles tasks such as data loading etc that need to be completed before the controls are rendered

6. Page.PreRenderComplete - it is fired once the OnPreRender gets executed

7. Render - Renders the entire control

8. RenderContents - Renders the contents

Diff between Microsoft.SharePoint.WebPartPages and System.Web.UI.WebControls.WebParts

Microsoft.SharePoint.WebPartPages provides backward compatibility with the webparts of SharePoint 2003 in MOSS 2007.  In case we plan to create a webpart in MOSS 2007 with nothing to do with the previous versions, it's recommended to use System.Web.UI.WebControls.WebParts.

Types of WebParts

They can be braodly classified in to 2:

1. ASP .NET WebParts: These are built on top of the ASP .NET WebPart Infrastructure.  They need a reference to System.Web.dll and inherit from the webpart base class System.Web.UI.WebControls.WebParts namespace.  These can be used in ASP .NET applications as well as SharePoint sites.

2. SharePoint WebParts:  These need a reference from the Microsoft.SharePoint.dll and must inherit the base class Microsoft.SharePoint.WepPartPages namespace.  These can be used in Sharepoint Sites only.

SPVirtualPathProvider

Further strenghtening our understanding of Ghosting and Unghosting concept posted here - http://sharepointbunker.blogspot.com/2011/02/understanding-site-definitions.html

As already mentioned, Ghosting helpes in maintaining only one version of all uncustomized pages.  For example, every site has a Default.aspx that is not replicated.  Hence saving space and maintains a leaner server.  Imagine the state of the server if every site in the web application had an individual copy of Deafult.aspx saved on the server!!!

So what happens is that the common template of Default.aspx is loaded on to the memory and every site that has an uncustomized version of Default.aspx references to that.

But what happens when a page is customized!!  That cannot be maintained as a common template for all to reference to!!  This is where Unghosting and SPVirtualPathProvider comes in to picture.  The customized page gets unghosted.  It then gets saved to the content database.  And then a virtual path is provided pointing to page in the content database.

How to get Code Snippets to look good on your blogger

Was struggling quite a bit to get the snippets to look readable.  Came across these then - http://www.craftyfella.com/2010/01/syntax-highlighting-with-blogger-engine.html and http://formatmysourcecode.blogspot.com/

Quite helpful.

If any one has better ideas, do post them in the comments.

<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>  
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>  
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'></script>  
<script language='javascript'>  SyntaxHighlighter.config.bloggerMode = true; SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf'; SyntaxHighlighter.all(); </script>

1. Paste it into your Blogger Template just above the </head> tag
2. Save the template
3. Then you can start creating code blocks in your existing or new Blog entries.

There are 2 ways to add a code block using syntaxhighlighter

Method 1: Using the script Tag
<script type=&quot;syntaxhighlighter&quot; class=&quot;brush: csharp&quot;>
<![CDATA[ // Comment
public class Testing {
public Testing() {} 
public void Method() 
{/* Another Commenton multiple lines */int x = 9;}
}]]></script>

becomes
// Comment
public class Testing {    
public Testing() {    
}     

public void Method() {        
/* Another Comment           
on multiple lines */        
int x = 9;    
}
}

Method 2: Using the pre Tag
<pre class="brush: csharp">// Comment
public class Testing {
public Testing() {
} 

public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}
</pre>

becomes
// Comment 
public class Testing {     
public Testing() {     
}       

public void Method() {         
/* Another Comment            
on multiple lines */        
int x = 9;     
} 
}

Check a Parent node and all Child nodes get checked in TreeView using JS

Had this requirement in my project.  Got a great post here - http://pushpontech.wordpress.com/2007/06/06/aspnet-20-treeview-checkbox-checkuncheck-all-script/  and http://forums.asp.net/t/1367074.aspx

function OnTreeClick(evt){
var src = window.event != window.undefined ? window.event.srcElement : evt.target;
var isChkBoxClick = (src.tagName.toLowerCase() == “input” && src.type == “checkbox”);
if(isChkBoxClick)
{
var parentTable = GetParentByTagName(“table”, src);
var nxtSibling = parentTable.nextSibling;
//check if nxt sibling is not null & is an element node
if(nxtSibling && nxtSibling.nodeType == 1)
{
if(nxtSibling.tagName.toLowerCase() == “div”) //if node has children
{
//check or uncheck children at all levels
CheckUncheckChildren(parentTable.nextSibling, src.checked);
}
}
//check or uncheck parents at all levels
CheckUncheckParents(src, src.checked);
}
}
 function CheckUncheckChildren(childContainer, check)
{
var childChkBoxes = childContainer.getElementsByTagName(“input”);
var childChkBoxCount = childChkBoxes.length;
for(var i=0;i<checkBoxCount;i++)
{
childChkBoxes[i].checked = check;
}
}
function CheckUncheckParents(srcChild, check)
{
var parentDiv = GetParentByTagName(“div”, srcChild);
var parentNodeTable = parentDiv.previousSibling;
if(parentNodeTable)
{
var checkUncheckSwitch;
if(check) //checkbox checked
{
var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);
if(isAllSiblingsChecked)
checkUncheckSwitch = true;
else
return; //do not need to check parent if any(one or more) child not checked
}
else //checkbox unchecked
{
checkUncheckSwitch = false;
}var inpElemsInParentTable = parentNodeTable.getElementsByTagName(“input”);
if(inpElemsInParentTable.length > 0)
{
var parentNodeChkBox = inpElemsInParentTable[0];
parentNodeChkBox.checked = checkUncheckSwitch;
//do the same recursively
CheckUncheckParents(parentNodeChkBox, checkUncheckSwitch);
}
}
}function AreAllSiblingsChecked(chkBox)
{
var parentDiv = GetParentByTagName(“div”, chkBox);
var childCount = parentDiv.childNodes.length;
for(var i=0;i<childCount;i++)
{
if(parentDiv.childNodes[i].nodeType == 1)
{
//check if the child node is an element node
if(parentDiv.childNodes[i].tagName.toLowerCase() == “table”)
{
var prevChkBox = parentDiv.childNodes[i].getElementsByTagName(“input”)[0];
//if any of sibling nodes are not checked, return false
if(!prevChkBox.checked)
{
return false;
}
}
}
}
return true;
}