Monday, September 28, 2009

Hide Content Place Holder Programmatically

I was working on replacing a control from the master page by a custom web part. Initially i commented everything inside the content place holder and added my web part. If you want to know how to add a web part in the master page, have a look at my previous post at :

http://www.sharepointkings.com/2009/09/add-web-part-inside-master-page-in-moss.html

Later on i moved the web part reference out of the content place holder and made the visible attribute of the content place holder to false.

But now the requirement is to dynamically turn it ON and OFF (i.e through code)

Lets follow the steps given below to achieve this:

1. Go to the page_load method of the web part.
2. Write the following lines of code.


if (!Page.IsPostBack)
{
ContentPlaceHolder contPlcHolder = (ContentPlaceHolder)Page.Master.FindControl("PlaceHolderGlobalNavigation");
contPlcHolder.Visible = false;

//Rest of the code

}


If there is any nested content place holder then append that number of FindControl("ControlName") to the above code where we are finding the control.

That's it. Save the code and deploy the latest web part code.

If you are facing any issue then email us at sharepointkings@gmail.com

Sunday, September 13, 2009

Add Web Part inside a Master Page in MOSS 2007

Imagine that you are designing a master page for your MOSS site and you have a requirement to have a functionality that is not possible out of the box. You brain storm on it and concludes that only custom web part can solve your problem.
But now your concern is how you can make this custom web part as part of your master page?
It’s easy! Just follow the steps given below to add a web part inside a master page.

1. Deploy the web part in your web application as you do normally.
2. Check whether the web part is showing up in the web part gallery.
3. Create a web part page by navigating to the site settings, create web part page.
4. Add your custom web part on the web part page.
5. Start SharePoint designer and open the newly created web part page.
6. You will now find two entries corresponding to the web part you added on the page
i. In the page declaration you will find a entry which looks something like this:


<%@ Register TagPrefix="WpNs0" Namespace="MyNamespace.MyClass" Assembly=" MyNamespace.MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"%>

ii. Click on the web part section in the page and you will see an entry like this:


<WpNs0:WebPartName runat="server" ID="g_34cdbb90_38c6_4561_9182_8fbc0d2423a6" ExportMode="All" Title="WebPartTitle" __MarkupType ……………..

7. Copy these entries in separate notepad, and close the page opened in the SharePoint designer.
8. Now open the master page in SharePoint designer and add the first entry in the declaration section and second entry at the place where you want to add the web part to.
9. Save the page, check in and publish it and that’s it. Your job is done.


Do let us know in case you are facing any issue following the steps mentioned.
You can email us at sharepointkings@gmail.com

Sunday, September 6, 2009

Creating and working with Survey in SharePoint - Part 5

Hi All,

In this article we will discuss about how to hide the save button. You may wonder why we should hide the save button. We will hide the save button to avoid one big issue of the survey.

If you have not gone through the survey articles, I would recommend you to read them all first before you read further.

Creating and working with Survey in SharePoint - Part 1

Creating and working with Survey in SharePoint - Part 2

Creating and working with Survey in SharePoint - Part 3

Creating and working with Survey in SharePoint - Part 4

When you have any question that has branching, SharePoint automatically places the next question on the second page and gives you an option to save your response so that later you can edit it. If you have the long survey that has many branching questions, then SharePoint does this splitting of page between the questions. Here we have one big problem. When responder saves the response, you will see that number of response increases. If you have five responders and all five of them have saved the respond, not completed the respond, then first thing you will notice is you can see total number of responses as five, but when you will actually go to the survey and find out then you won’t see even a single response. So where are all those responses?

Well, answer to this question is really simple. Even though you have full rights on survey or even you are a site administrator or site collection administrator, nobody can see those responses except a person who has saved the response. Only he can see his saved response. So bottom line is check columns named “Completed” in “All Responses” view, you will find only those responses whose status is yes. If a person has saved response, he will see status as “no” in “Completed” column.

Now why we are talking about hiding the save button is because there can be a reason where in you want responders to respond the survey in specific days. If they don’t then you want to send mail to them one more time to indicate that look, you need to fill this. Now to know this that how many people have not responded to survey yet, is don’t allow to save the response so that you can have exact number of responses.

Ok, so let us go ahead and add the content editor web part in every page of your questions, so that save button doesn’t appear on each and every page of survey. So now responder has no choice but to fill in the survey at one go.

In content editor web part, place the following code.

<script language="JavaScript">
var ClassName;
ClassName = 'ms-toolbar';

var elements = new Array();

var elements = document.getElementsByTagName('td');

for(var e=0;e<elements.length;e++)
{
if(elements[e].className == ClassName)
{

elements[e].innerHTML = elements[e].innerHTML.replace('value=Save','value=Save style=visibility:hidden ');



}

}

</script>


Your job is done. Now see the effect of above code.



That's it.

Saturday, September 5, 2009

Creating and working with Survey in SharePoint - Part 4

Hi All,

Here I am going to discuss something interesting especially on overview.aspx of survey list. If you have not gone through the survey articles, I would recommend you to read them all first before you read further.

Creating and working with Survey in SharePoint - Part 1

Creating and working with Survey in SharePoint - Part 2

Creating and working with Survey in SharePoint - Part 3

and

Creating and working with Survey in SharePoint - Part 5


One question came in my mind. Why any responder of survey should care about knowing when survey was created and how many number of responses has come so far. If we have set the permission in advance settings of survey that user can only see their response, and then also responder can see how many responses have come so far.

If we do not want to allow any responder to see when survey was created and how many responses has come so far, then do one very easy thing. Just add one more content editor web part on the overview.aspx page of survey and copy and paste the below code and there you go, you have each and every option just like you have in normal survey web part on overview.aspx. The only difference is that here you cannot see number of responses and date created. Make sure that you hide the default web part on that page by modifying that web part and setting hidden to true under layout option. So simple…isn’t it.

Just make sure that you replace the {siteurl} for HREF in below code. Just place your site url there and you should be good to go.


<table class="ms-menutoolbar" cellpadding="2" cellspacing="0" border="0" id="ctl00_m_g_c84f415c_dba8_4240_b34d_263e596c7eed_ctl00_ctl00_toolBarTbl" width="100%" >
<tr> <td class="ms-toolbar" nowrap="true">
<div class="ms-buttoninactivehover" onmouseover="this.className='ms-buttonactivehover'" onmouseout="this.className='ms-buttoninactivehover'"><a id="ctl00_m_g_c84f415c_dba8_4240_b34d_263e596c7eed_ctl00_ctl00_toolBarTbl_RptControls_ctl00_diidIONewItem" accesskey="N" title="Respond to this Survey" onclick="javascript:NewItem('\u002fsites\u002fTest\u002fLists\u002fSharePoint\u002520site\u002520survey\u002fNewForm.aspx');return false;" href="javascript:__doPostBack('ctl00$m$g_c84f415c_dba8_4240_b34d_263e596c7eed$ctl00$ctl00$toolBarTbl$RptControls$ctl00$diidIONewItem','');"><img align='absmiddle' alt="Respond to this Survey" src="/_layouts/images/NewItem.gif" style='border-width:0px;'>&nbsp;Respond to this Survey</a></div>
</td>
<td class=ms-separator><img src='/_layouts/images/blank.gif' alt=''></td>
<td class="ms-toolbar" nowrap="true">


<span style="display:none"><menu type='ServerMenu' id="zz13_RptControls" largeIconMode="true"><ie:menuitem id="zz14_ExportToSpreadsheet" type="option" iconSrc="/_layouts/images/MenuSpreadsheet.gif" onMenuClick="javascript:EnsureSSImporter();javaScript:ExportList('\u002fsites\u002fTest\u002f_vti_bin\u002fowssvr.dll?CS=65001\u0026Using=_layouts\u002fquery.iqy\u0026List=\u00257BA1B0E302\u00252D497E\u00252D45B9\u00252D9994\u00252D9F0E0171A380\u00257D\u0026View=\u00257BC84F415C\u00252DDBA8\u00252D4240\u00252DB34D\u00252D263E596C7EED\u00257D\u0026RootFolder=\u00252Fsites\u00252FTest\u00252FLists\u00252FSharePoint\u002520site\u002520survey\u0026CacheControl=1')" text="Export to Spreadsheet" description="Analyze items with a spreadsheet application." menuGroupId="800"></ie:menuitem><ie:menuitem id="ctl00_m_g_c84f415c_dba8_4240_b34d_263e596c7eed_ctl00_ctl00_toolBarTbl_RptControls_ctl01_ctl00_ctl03" type="separator"></ie:menuitem><ie:menuitem id="zz15_ViewRSS" type="option" iconSrc="/_layouts/images/MenuRSS.gif" onMenuClick="window.location = '/sites/Test/_layouts/listfeed.aspx?List=%7BA1B0E302%2D497E%2D45B9%2D9994%2D9F0E0171A380%7D';" text="View RSS Feed" description="Syndicate items with an RSS reader." menuGroupId="800"></ie:menuitem><ie:menuitem id="zz16_SubscribeButton" type="option" iconSrc="/_layouts/images/MenuAlert.gif" onMenuClick="window.location = '/sites/Test/_layouts/SubNew.aspx?List=%7BA1B0E302%2D497E%2D45B9%2D9994%2D9F0E0171A380%7D&amp;Source=http%3A%2F%2Fsharepointkings%3A7777%2Fsites%2FTest%2FLists%2FSharePoint%2520site%2520survey%2Foverview%2Easpx';" text="Alert Me" description="Receive e-mail notifications when items change." menuGroupId="800"></ie:menuitem></menu></span><span title="Open Menu"><div id="zz17_ListActionsMenu_t" class="ms-menubuttoninactivehover" onmouseover="MMU_PopMenuIfShowing(this);MMU_EcbTableMouseOverOut(this, true)" hoverActive="ms-menubuttonactivehover" hoverInactive="ms-menubuttoninactivehover" onclick=" MMU_Open(byid('zz13_RptControls'), MMU_GetMenuFromClientId('zz17_ListActionsMenu'),event,false, null, 0);" foa="MMU_GetMenuFromClientId('zz17_ListActionsMenu')" oncontextmenu="this.click(); return false;" nowrap="nowrap"><a id="zz17_ListActionsMenu" accesskey="C" href="#" onclick="javascript:return false;" style="cursor:hand;white-space:nowrap;" onfocus="MMU_EcbLinkOnFocusBlur(byid('zz13_RptControls'), this, true);" onkeydown="MMU_EcbLinkOnKeyDown(byid('zz13_RptControls'), MMU_GetMenuFromClientId('zz17_ListActionsMenu'), event);" onclick=" MMU_Open(byid('zz13_RptControls'), MMU_GetMenuFromClientId('zz17_ListActionsMenu'),event,false, null, 0);" oncontextmenu="this.click(); return false;" menuTokenValues="MENUCLIENTID=zz17_ListActionsMenu,TEMPLATECLIENTID=zz13_RptControls" serverclientid="zz17_ListActionsMenu">Actions<img src="/_layouts/images/blank.gif" border="0" alt="Use SHIFT+ENTER to open the menu (new window)."/></a><img align="absbottom" src="/_layouts/images/menudark.gif" alt="" /></div></span>
</td>
<td class=ms-separator><img src='/_layouts/images/blank.gif' alt=''></td>
<td class="ms-toolbar" nowrap="true">


<span style="display:none"><menu type='ServerMenu' id="zz18_RptControls" largeIconMode="true"><ie:menuitem id="zz19_AddQuestions" type="option" onMenuClick="window.location = '/sites/Test/_layouts/qstNew.aspx?List=%7BA1B0E302%2D497E%2D45B9%2D9994%2D9F0E0171A380%7D&amp;Source=http%3A%2F%2Fsharepointkings%3A7777%2Fsites%2FTest%2FLists%2FSharePoint%2520site%2520survey%2Foverview%2Easpx';" text="Add Questions" description="Add an additional question to this survey." menuGroupId="100"></ie:menuitem><ie:menuitem id="zz20_ListSettings" type="option" iconSrc="/_layouts/images/MenuListSettings.gif" onMenuClick="window.location = '/sites/Test/_layouts/survedit.aspx?List=%7BA1B0E302%2D497E%2D45B9%2D9994%2D9F0E0171A380%7D';" text="Survey Settings" description="Manage questions and settings for this survey." menuGroupId="200"></ie:menuitem></menu></span><span title="Open Menu"><div id="zz21_ListSettingsMenu_t" class="ms-menubuttoninactivehover" onmouseover="MMU_PopMenuIfShowing(this);MMU_EcbTableMouseOverOut(this, true)" hoverActive="ms-menubuttonactivehover" hoverInactive="ms-menubuttoninactivehover" onclick=" MMU_Open(byid('zz18_RptControls'), MMU_GetMenuFromClientId('zz21_ListSettingsMenu'),event,false, null, 0);" foa="MMU_GetMenuFromClientId('zz21_ListSettingsMenu')" oncontextmenu="this.click(); return false;" nowrap="nowrap"><a id="zz21_ListSettingsMenu" accesskey="I" href="#" onclick="javascript:return false;" style="cursor:hand;white-space:nowrap;" onfocus="MMU_EcbLinkOnFocusBlur(byid('zz18_RptControls'), this, true);" onkeydown="MMU_EcbLinkOnKeyDown(byid('zz18_RptControls'), MMU_GetMenuFromClientId('zz21_ListSettingsMenu'), event);" onclick=" MMU_Open(byid('zz18_RptControls'), MMU_GetMenuFromClientId('zz21_ListSettingsMenu'),event,false, null, 0);" oncontextmenu="this.click(); return false;" menuTokenValues="MENUCLIENTID=zz21_ListSettingsMenu,TEMPLATECLIENTID=zz18_RptControls" serverclientid="zz21_ListSettingsMenu">Settings<img src="/_layouts/images/blank.gif" border="0" alt="Use SHIFT+ENTER to open the menu (new window)."/></a><img align="absbottom" src="/_layouts/images/menudark.gif" alt="" /></div></span>
</td>

<td width="99%" class="ms-toolbar" nowrap><IMG SRC="/_layouts/images/blank.gif" width=1 height=18 alt=""></td>


<td class="ms-toolbar" nowrap="true">

<table border=0 cellpadding=0 cellspacing=0 style='margin-right: 4px'>
<tr>
<td nowrap class="ms-toolbar" id="topPagingCellWPQ2">
<td>
</tr>
</table>

</td>
<td class=ms-separator> </td>
<td class="ms-toolbar" nowrap="true">

<table border=0 cellpadding=0 cellspacing=0 style='margin-right: 4px'>
<tr>
<td nowrap class="ms-listheaderlabel">View:&nbsp;</td>
<td id="ctl00_m_g_c84f415c_dba8_4240_b34d_263e596c7eed_ctl00_ctl00_toolBarTbl_RightRptControls_ctl01_ctl00_onetViewSelector" nowrap="nowrap" class="ms-viewselector" onmouseover="this.className='ms-viewselectorhover'" onmouseout="this.className='ms-viewselector'">
<span style="display:none"><menu type='ServerMenu' id="zz22_ViewSelectorMenu" CompactMode="true"><ie:menuitem id="zz23_DefaultView" type="option" onMenuClick="window.location = '/sites/Test/Lists/SharePoint site survey/overview.aspx';" text="Overview" menuGroupId="100"></ie:menuitem><ie:menuitem id="zz24_View1" type="option" onMenuClick="window.location = '/sites/Test/Lists/SharePoint site survey/AllItems.aspx';" text="All Responses" menuGroupId="300"></ie:menuitem><ie:menuitem id="zz25_View2" type="option" onMenuClick="window.location = '/sites/Test/Lists/SharePoint site survey/summary.aspx';" text="Graphical Summary" menuGroupId="300"></ie:menuitem></menu></span><span title="Open Menu"><div id="zz26_ViewSelectorMenu_t" class="ms-viewselector" onmouseover="MMU_PopMenuIfShowing(this);MMU_EcbTableMouseOverOut(this, true)" hoverActive="ms-viewselectorhover" hoverInactive="ms-viewselector" onclick=" MMU_Open(byid('zz22_ViewSelectorMenu'), MMU_GetMenuFromClientId('zz26_ViewSelectorMenu'),event,true, 'ctl00_m_g_c84f415c_dba8_4240_b34d_263e596c7eed_ctl00_ctl00_toolBarTbl_RightRptControls_ctl01_ctl00_onetViewSelector', 0);" foa="MMU_GetMenuFromClientId('zz26_ViewSelectorMenu')" oncontextmenu="this.click(); return false;" nowrap="nowrap"><a id="zz26_ViewSelectorMenu" accesskey="W" href="#" onclick="javascript:return false;" style="cursor:hand;white-space:nowrap;" onfocus="MMU_EcbLinkOnFocusBlur(byid('zz22_ViewSelectorMenu'), this, true);" onkeydown="MMU_EcbLinkOnKeyDown(byid('zz22_ViewSelectorMenu'), MMU_GetMenuFromClientId('zz26_ViewSelectorMenu'), event);" onclick=" MMU_Open(byid('zz22_ViewSelectorMenu'), MMU_GetMenuFromClientId('zz26_ViewSelectorMenu'),event,true, 'ctl00_m_g_c84f415c_dba8_4240_b34d_263e596c7eed_ctl00_ctl00_toolBarTbl_RightRptControls_ctl01_ctl00_onetViewSelector', 0);" oncontextmenu="this.click(); return false;" menuTokenValues="MENUCLIENTID=zz26_ViewSelectorMenu,TEMPLATECLIENTID=zz22_ViewSelectorMenu" serverclientid="zz26_ViewSelectorMenu">Overview<img src="/_layouts/images/blank.gif" border="0" alt="Use SHIFT+ENTER to open the menu (new window)."/></a><img align="absbottom" src="/_layouts/images/blank.gif" alt="" /></div></span>
</td>

</tr>
</table>

</td>

</tr>
</table>


<TABLE class="ms-summarystandardbody" cellpadding=0 cellspacing=0 width=600px style="margin: 10px;" border=0 rules=rows> <TR> <TD class="ms-formlabel" width=190px ID="overview01">Survey Name:</TD> <TD class="ms-formbody" >Movie knowledge survey</TD> </TR> <TR> <TD class="ms-formlabel" valign="top" ID="overview02">Survey Description:</TD> <TD class="ms-formbody">This is the startbold Hindi endbold as well as the startitalic English movie enditalic Knowledge survey. linebreak

we would like to know how much people are startunderline aware about the Hindi and English movies endunderline</TD> </TR> <TR> <TD><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD> </TR> </TABLE>
<TABLE border=0 style="margin: 0px 8px 0px 8px;">
<tr><td><img src="/_layouts/images/blank.gif" width=1 height=4 alt=""></td></tr>
<tr>
<td nowrap>
<img src="/_layouts/images/rect.gif" alt=""><span class=ms-toolbar>&nbsp;</span>
<a class="ms-toolbar" ACCESSKEY=R ID=diidSurveyResults HREF="{siteurl}/Lists/SharePoint site survey/summary.aspx"><!-- _locID_text="overview05" _locComment="{StringCategory=TXT}"-->Show a graphical summary of responses</a>
<span class=ms-toolbar>&nbsp;</span>
</td>
</tr>
<tr>
<td nowrap>
<img src="/_layouts/images/rect.gif" alt=""><span class=ms-toolbar>&nbsp;</span>
<a class="ms-toolbar" ACCESSKEY=U ID=diidResultsByUser HREF="{siteurl}Lists/SharePoint site survey/AllItems.aspx"><!-- _locID_text="overview06" _locComment="{StringCategory=TXT}"-->Show all responses</a>
<span class=ms-toolbar>&nbsp;</span>
</td>
</tr>
</TABLE>


And see the effect of the above code.




That's it.

Creating and working with Survey in SharePoint - Part 3

Hi All,

We are going to discuss more on survey in this article. If you have not gone through the previous articles, I would suggest you to go through those articles first and then proceed for reading.

If you haven't gone through the previous articles of this series, i would highly recommend to read following articles before proceeding.

Creating and working with Survey in SharePoint - Part 1

Creating and working with Survey in SharePoint - Part 2

and

Creating and working with Survey in SharePoint - Part 4

Creating and working with Survey in SharePoint - Part 5

Now, that we have survey list already created and if you see the description of the survey on the overview.aspx, there is a difference in the survey description in the webpart and survey description below survey name. Check this out.



The difference is the line break. If you observe, survey description in the web part is written as it is without any line break however survey description under the name of survey breaks two separates sentences into two different lines because when we created the survey we gave it like that.

So in this article we will discuss two important things that will help you not only for the survey but also for any other list and any other document library as well.

Here we will remove the survey description below the name of the survey because we can already see the description in the web part and also we will include the HTML in the description now. It is very much similar to what we have learnt in previous articles just a class name change and that will do our job. Let us see in detail.

Ok, Edit the overview page and place the content editor web part below to the original web part.



Ok, first let us go and change the description of survey. Go to settings, survey settings, title and description and change the description to the following.

This is the startbold Hindi endbold as well as the startitalic English movie enditalic Knowledge survey. linebreak


we would like to know how much people are startunderline aware about the Hindi and English movies endunderline.

Ok, now go back to the overview view of the survey and observe the survey description.



As you can see those words start appearing in the description, so we need to get rid of it and replace them with the actual HTML behavior and that is what we did in previous articles. So go ahead and modify the content editor web part and place the following code in that. Note one important line in the below code and that line is where we assign “ms-formbody’ class. Earlier we assigned “ms-formlabel” in previous articles to add HTML in questions.

<script language="JavaScript">
var ClassName;
ClassName = 'ms-formbody';

var elements = new Array();

var elements = document.getElementsByTagName('td');

for(var e=0;e<elements.length;e++)
{
if(elements[e].className == ClassName)
{
elements[e].innerHTML = elements[e].innerHTML.replace('linebreak','<br/>');

elements[e].innerHTML = elements[e].innerHTML.replace('startunderline','<u>');

elements[e].innerHTML = elements[e].innerHTML.replace('endunderline','</u>');

elements[e].innerHTML = elements[e].innerHTML.replace('startitalic','<i>');

elements[e].innerHTML = elements[e].innerHTML.replace('enditalic','</i>');

elements[e].innerHTML = elements[e].innerHTML.replace('startbold','<b>');

elements[e].innerHTML = elements[e].innerHTML.replace('endbold','</b>');

}

}

</script>



And there you go, see the effect of it.



Ok, look good now. But there is a problem here, still look at the survey description above the web part. It still shows the words (startbold, endbold etc). They have not been replaced yet.

How to replace them? Ok, here is the answer. Just add one more content editor web part below the previous content editor web part and copy the same HTML content above, just make two changes. Change the class name and getElemtByID from TD to Div as this description is in the div tag, not in the TD tag.

ClassName = 'ms-listdescription'
var elements = document.getElementsByTagName('div');


and there you go, see the magic.



Actually for this specific survey type of list, we don’t need this to be here as on the same page, we can see the survey description in the web part. So let us go and remove the description of survey below the name of the survey from overview.aspx.

Modify the last content editor web part and paste the following code inside it.

<script language="JavaScript">
var ClassName;
ClassName = 'ms-listdescription';

var elements = new Array();

var elements = document.getElementsByTagName('div');

for(var e=0;e<elements.length;e++)
{
if(elements[e].className == ClassName)
{

elements[e].innerHTML = '';
}

}

</script>


And see the effect of above code. Survey description is gone.



Remember one very important point. You need to place this last content editor web part in every view page. We placed it only in overview.aspx. Just try and change the view to “All responses” and “Graphical summary”. Survey description will appear there, so just make sure that you place the last content editor web part on each view page separately as well.



Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com