Sunday, May 27, 2012

Manual steps to migrate a customized sharepoint 2007 site definition to SharePoint 2010 site definetion(Visual Studio Code Project)


Migrating existing SharePoint 2007 project to SharePoint 2010
Step1: Create SiteDefintion Project
1    a.)   Start Visual Studio and create a “Empty SharePoint Project”.
2    b.)  Add  a Sharepoint Mapped folder and map it to “SharePointRoot”.
                
             

            c.)  After mapping it will create” SharePointRoot” folder in your project, Now copy the “Template” folder from 2007 project and paste it in “SharePointRoot” folder” 
      d.) Now Include “Template” folder in project 
                                                                                                   
        
      e.) Since existing master page is of Moss 2007 so it needs to be changed. To change the master change open and existing dummy sharepoint site in SharePoint Designer and copy the content of v4.master and replace  it in current master page.
             f.) Please repeat the above step to change the default.aspx
      g.) Now to change the schema.xml of each list. Go to individual list(SharePointRool\Template\SiteTemplate\(“SiteDefinitionName”)\List\ListName).
            h.) If you are not customizing any List forms |(NewForm/EditForm/DispForm) then don’t include them at list level, They will automatically get generated at runtime(Same applies for Views).
            i.) Schema.xml should follow below structure
<Listxmlns:ows="Microsoft SharePoint"Title="List Title"NavigateForFormsPages="TRUE"FolderCreation="FALSE"Direction="$Resources:Direction;"Url="Lists/ListName"BaseType="0"xmlns="http://schemas.microsoft.com/sharepoint/">
<MetaData>
              <ContentTypes>
      
</ContentTypes>
              <Fields>
</Fields>
<Views>
                     <ViewDisplayName="All Items"
DefaultView="TRUE"BaseViewID="1"Type="HTML"MobileView="TRUE"MobileDefaultView="TRUE"ImageUrl="/_layouts/images/generic.png"XslLink="main.xsl"WebPartZoneID="Main"WebPartOrder="1"Url="AllItems.aspx"SetupPath="pages\viewpage.aspx">
<XslLink>main.xsl</XslLink>
<ToolbarType="Standard" />

              <Query>
<OrderBy>
                     <FieldRefName="ID" />
                     </OrderBy>
              </Query>
              <ViewFields>
                     <FieldRefName="Title" />
                     <FieldRefName="FieldName" />
              </ViewFields>
              <RowLimitPaged="TRUE">30</RowLimit>
                     </View>
              </Views>
       <Forms>
              <FormType="DisplayForm"Url="DispForm.aspx"
SetupPath="pages\form.aspx"WebPartZoneID="Main" />
<FormType="EditForm"Url="EditForm.aspx"
SetupPath="pages\form.aspx"WebPartZoneID="Main" />
              <FormType="NewForm"Url="NewForm.aspx"
SetupPath="pages\form.aspx"WebPartZoneID="Main" />
       </Forms>

</MetaData>
</List>


            j.) If you are customizing the form pages then don’t specify  SetupPath” attribute under <Forms> section.
           k.) Add the customized pages at schema.xml level.
            l.) Refer NewForm.aspx/EditForm.aspx/DispForm.aspx for customizing it. You can use highlighted path to get reference for customized form and view page  (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\Pages).


Step2: Create CodeBehind  project        
           a.) Add  a “Empty SharePoint Project” in  above solution
           b.) Create a “Code Library” Folder and copy all the existing code library files in this folder.
           c.)  Include all the files in the project
           d.) Add “ListReceiver” named “ClearCacheReceiver”  and copy all the code from existing project (Moss 2007)
e






Friday, May 18, 2012

How to create a dynamic caml query on runtime with multiple Or or And.

Suppose you want to get all the items from a Sharepoint list where one column which is named as city is equal  to 'X', 'Y' or 'Z', you can get it by adding OR again and again to query.  But suppose you don't know that through which values you are going to compare it or going to get it on run time then you need to create your query at run time. So here is the way to create a CAML query in this scenario. Here I am going to generate it for so many OR and with one AND. Here you can get query for all the items from a list where Country is India and City can be any as from a List<string> which you can populate based on your conditions or requirements.

public StringBuilder GetQueryForAllDaysTCodes(List<string> Cities)
        {
string startingQuery = "<Where><And><Eq><FieldRef Name='Countries' /><Value Type='Text'>India</Value></Eq>";
            StringBuilder stbQuery = new StringBuilder();
            stbQuery.Append(startingQuery);
            bool first = true;
            foreach (string City in Cities)
            {
                if (Cities.Length > 0)
                {
                    if (first)
                    {
                        stbQuery.Insert(startingQuery.Length, "<Eq><FieldRef Name='Cities' /><Value Type='Text'>").Append(City.Trim()).Append("</Value></Eq>");
                        first = false;
                    }
                    else
                    {
                        stbQuery.Insert(startingQuery.Length, "<Or>");
                        stbQuery.Append("<Eq><FieldRef Name='Cities' /><Value Type='Text'>").Append(City.Trim()).Append("</Value></Eq>");
                        stbQuery.Append("</Or>");
                    }
                }
            }
            stbQuery.Append("</And></Where>");
            return stbQuery;
        }

Note: Remove highlighted text to get only an OR condition query. 

How to add customized tooltip to a gridview row and change hovred row colour.

In Page.aspx, put these line of codes to add a div and label. Label will store the value to show in ToolTip and div will work as a container. You can bind any value in this label from table or in run time and can change CssClasses as per your requirement :

<div id="divComments" runat="server" class="TooltipDiv"  style="display:none;"> 
<asp:Label runat="server" ID="lblComments" Text='<%#DataBinder.Eval(Container.DataItem,"Comments") %>' CssClass="TooltipText"></asp:Label></div>

Now put these java script functions to page to handle mouse out and hover event:

function showToolTip(divId, linkId) {
            if (null != divId) {
                var elementRef = document.getElementById(divId);
                if (elementRef != null) {
                    elementRef.style.position = 'absolute';
                    elementRef.style.left = event.clientX + 5 + document.body.scrollLeft;
                    elementRef.style.top = event.clientY + 5 + document.body.scrollTop;
                    elementRef.style.visibility = 'visible';
                    elementRef.style.display = 'block';
                }
                if (null != linkId) {
                    var elementBak = document.getElementById(linkId);
                    if (elementBak != null) {
                        elementBak.style.backgroundColor = '#d9d9d9';
                    }
                }
            }
        }

        function hideToolTip(divId, linkId) {
            if (null != divId) {
                var elementRef = document.getElementById(divId);
                if (elementRef != null) {
                    elementRef.style.visibility = 'hidden';
                }
            }
            if (null != linkId) {
                var elementBak = document.getElementById(linkId);
                if (elementBak != null)
                {
                    elementBak.style.backgroundColor = '#ffffff';
                }
            }
        }

After this put these code in the grdView_RowDataBound event within Page.aspx.cs, it will find current row and call this java script function for it. :

HtmlContainerControl addToolTipDiv = (HtmlContainerControl)e.Row.FindControl("divComments");
                        e.Row.Attributes.Add("onmouseover", "showToolTip('" + addToolTipDiv.ClientID + "','" + e.Row.ClientID + "');");
                        e.Row.Attributes.Add("onmouseout", "hideToolTip('" + addToolTipDiv.ClientID + "','" + e.Row.ClientID + "');");

If you want to add only background change of hovered row then call it in this way. It will pass no div Id so there will be no tool tip added.

e.Row.Attributes.Add("onmouseover", "showToolTip('" + " "+ "','" + e.Row.ClientID + "');");
  e.Row.Attributes.Add("onmouseout", "hideToolTip('" +" " + "','" + e.Row.ClientID + "');");