Tuesday, December 27, 2011

Hide Columns in NewForm.aspx / EditForm.aspx / DispForm.aspx

Hide SharePoint Columns in New / Edit / View Form
Open the SharePoint Designer 2010, Go to the Corresponding List/ Library.

Open the New/Edit / View Item Aspx Page in Edit In Advance Mode add the Following script



<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">

Now copy & paste this JavaScript code after above mentioned tag :

<script language="javascript" type="text/javascript">
_spBodyOnLoadFunctionNames.push("hideFields");
function findacontrol(FieldName) {
   var arr = document.getElementsByTagName("!");
   // get all comments
   for (var i=0;i < arr.length; i++ )
   {
      // now match the field name
      if (arr[i].innerHTML.indexOf(FieldName) > 0)
      {         return arr[i];      }
   }
}

function hideFields() {
   var control = findacontrol("Title");
   control.parentNode.parentNode.style.display="none";
   control = findacontrol("Document Link");
   control.parentNode.parentNode.style.display="none";
   control = findacontrol("PublishDate");
   control.parentNode.parentNode.style.display="none";
}
</script>

Favicon icon in SharePoint 2010

Copy the download image to your layouts folder

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\IMAGES\CompanyName

Find the below in your master page and replace the new icon url.

<SharePoint:SPShortcutIcon runat="server" IconUrl="/_layouts/images/CompanyName/favicon.ico"/>


Create an ASMX Web Service in SharePoint 2010 Using Visual Studio 2010

Back in SharePoint 2007, asmx web services were quite prevalent, thanks to the WSPBuilder tool, and it’s templates.   They are useful for executing actions between multiple web applications and can be used by client applications, as well.  Furthermore, InfoPath forms, deployed to SharePoint, could also use these asmx web services.
Unfortunately, Visual Studio 2010 did not come with a template for SharePoint web services.  So, today I will be writing about how we can create asmx web services for SharePoint 2010.  All you will need is SharePoint 2010.
First, start a new Empty SharePoint 2010 project.  I will call this SPASMXService.
How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010_
Make sure to deploy it as a farm solution.
How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

First, you need to close this project by right clicking on the project and then selecting ‘unload project’.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010
Then, right click on the project again, and select, ‘Edit SPASMXService’.
How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010
Under <SandboxedSolution>False</SandboxedSolution> type in:

<TokenReplacementFileExtensions>asmx</TokenReplacementFileExtensions>

This will be the result:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

Then, save and close out of this xml file.  This will allow Visual Studio to insert the solution information where necessary.  Therefore, this is a crucial step!  Finally, reload the project file.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

Next, we will be creating the web service.  Right click on the project, and select “Add” and then select “New Class…”  This will be the code behind.  Let’s just call this SPASMXService.cs.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

Now, open SPASMXService.cs, and make the following changes:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

This is a good start for a web service with a web method.  Now, of course we have a few errors because we still have not brought in the necessary libraries.  Right click on ‘references’ in the Solution Explorer, and select, ‘Add Reference’.  Select System.Web.Services from the .NET tab.  Then, in SPASMXService.cs, add, ‘using System.Web.Services’.  This should look like this:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010
How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

Finally, we have to create the service page.  I like to keep it in the _layouts folder, but you can keep it elsewhere using similar steps.  Right click on the project item in the solution explorer, and select add -> SharePoint “Layouts” Mapped Folder.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

You can also select SharePoint Mapped Folder, and then select ISAPI.  This would cause the page to go into _vti_bin instead.

For now, I’m going to stick to _layouts:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

The SPASMXService folder was automatically made.  Nice.

Inside the SPASMXService, under Layouts, we will add a new file of type xml.  We Shall call it SPASMXService.asmx.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

The contents of SPASMXService.asmx will be a single line:

<%@ WebService Language="C#" Debug="true" Class="[Class path], $SharePoint.Project.AssemblyFullName$"  %>

Where [class path] is the full namespace name of the SPASMXService class in SPASMXService.cs.  In my case, the line will be:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

From:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

Finally, save everything, and then deploy the solution.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

If everything went right, you should see this using Internet Explorer:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010
If you used ISAPI instead of Layouts, _layouts in that screenshot should be _vti_bin, instead.  If you opened this from a front end server with web service, then you can further test this web service by clicking on that link.

Lastly, a bit of trouble shooting; you can check on the web service page by going to:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS

If you used ISAPI instead of LAYOUTS, then instead go to:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

If the web service does not load on Internet explorer, then you should open the asmx page from one of these two locations.  If you open the asmx page from one of these two locations, and you still find “$SharePoint.Project.AssemblyFullName$”, then you need to go back to the top of this article and follow the steps regarding unloading and reloading the project.

Reference From : http://www.thesharepointblog.net/Lists/Posts/Post.aspx?List=815f255a-d0ef-4258-be2a-28487dc9975c&ID=67

Create Groups In Drop Down List Box

<html>

<body>

<select>

<optgroup label="Numbers">

<option value="1">One</option>

<option value="2">Two</option>

<option value="3">Three</option>

</optgroup>

<optgroup label="Letters" disabled="true">

<option value="4">A</option>

<option value="5">B</option>

<option value="6">C</option>

</optgroup>

<optgroup label="Numbers set">

<option value="1">One</option>

<option value="2">Two</option>

<option value="3">Three</option>

</optgroup>

</select>

</body>

</html>   

Grouping items in a drop-down list box is easy with the optgroup element. Theoptgroup element is a child to the select element and has two attributes:label (required) and disabled (optional). The required label attribute specifies the text that will appear as a header to the contained group of options. Many browsers render the label text in bold italic. The optional disabled attribute is used to render the entire group of options non-selectable to the user. Options within an option group are indented.

ASP.NET 4.0 ClientID Overview

Introduction

One of the new features being added to version 4.0 of ASP.NET is the ability to control the client side IDs that are generated by the framework.  Previously the framework would modify the client side IDs to uniquely identify each control.  This some times left you with the ID you defined in markup or sometimes left you with something that looks like this, “ctl00_MasterPageBody_ctl01_Textbox1.”

The Problem

The modification of the client side id property works great to ensure that each element is uniquely identified, however, to anyone that has tried to do any sort of client side scripting this becomes very frustrating. Chances are that if you have worked in ASP.NET for any time at all you have run into this issue.  The problem is that until runtime you do not what the client side ID could be, making it difficult to do any kind of client side scripting.  In addition any modification of the page, adding removing controls, can result in a different client side ID being generated.

Old Solution

Again if you have worked with ASP.NET for any amount of time you know there is a work around for this issue.  Each control has a property called ClientID that is a read only and supplies the unique client side ID.  You can use this in a code behind when dynamically adding scripts, or more commonly use inline code (old ASP style) to supply the value to and client side scripts.
<script type="text/javascript">
    function DoSomething(){
        alert('<%= Control.ClientID %>');
    }
</script>

ASP.NET 4.0 Solution

First off let me start by explaining why we decided to tackle this problem in version 4.0 of the framework.  While we provided a way of supplying the developer with the client side ID, with the growth of client side scripting this solution has become some what hacky.  There is not really a clean way to use this with lots of controls and lots of external script files.  Also it might have had something to do with the developer asking for control over this.  Developers do love to have control of everything, weather they use it or not, it’s just our nature :) The solution that we came up has four ‘modes’ that a user can use giving them everything from existing behavior to full control.  The controls ID property is modified according to the ClientIDMode mode and then used as the client side id.

Modes and what they do

There is now a new property on every control (this includes pages and master pages as they inherit from control) called ClientIDMode that is used to select the behavior of the client side ID.
<asp:Label ID="Label1" runat="server" ClientIDMode="[Mode Type]" />
The Mode Types
  • Legacy: The default value if ClientIDMode is not set anywhere in the control hierarchy.  This causes client side IDs to behave the way they did in version 2.0 (3.0 and 3.5 did not change this code path) of the framework. This mode will generate an ID similar to “ctl00_MasterPageBody_ctl01_Textbox1.”
  • Inherit: This is the default behavior for every control.  This looks to the controls parent to get its value for ClientIDMode.  You do not need to set this on every control as it is the default, this is used only when the ClientIDMode has been changed and the new desired behavior is to inherit from the controls parent.
  • Static: This mode does exactly what you think it would, it makes the client side ID static. Meaning that what you put for the ID is what will be used for the client side ID.  Warning, this means that if a static ClientIDMode is used in a repeating control the developer is responsible for ensuring client side ID uniqueness.
  • Predictable: This mode is used when the framework needs to ensure uniqueness but it needs to be done so in a predictable way.  The most common use for this mode is on databound controls.  The framework will traverse the control hierarchy prefixing the supplied ID with it’s parent control ID until it reaches a control in the hierarchy whose ClientIDMode is defined as static.  In the event that the control is placed inside a databound control a suffix with a value that identifies that instance will also be added to the supplied ID.  The ClientIDRowSuffix property is used to control the value that will be used as a suffix (see samples).  This mode will generate an ID similar to “Gridview1_Label1_0”

Samples

Legacy Mode
Legacy mode is pretty straight forward, it generates a client side ID the way that it had in version 2.0 of the framework.
markup:
<asp :TextBox ID ="txtEcho" runat ="server" Width ="65%" ClientIDMode ="Legacy" /> 
output:
<input id="ctl00_MasterPageBody_ctl00_txtEcho" style="width: 65%" name="ctl00$MasterPageBody$ctl00$txtEcho" />
Static Mode
Static is the most basic of all ClientIDMode modes, what you give for the ID is what you get for the client side ID. Once again a warning that if a static ClientIDMode is used inside of a repeated control it is the developer’s responsibility to ensure client side ID uniqueness.
markup:
<asp:TextBox ID="txtEcho2" runat="server" Width="65%" ClientIDMode="Static" />
output:
<input id="txtEcho2" style="width: 65%" name="ctl00$MasterPageBody$ctl00$txtEcho2" />
Predictable Mode
Predictable mode really tackles the heart of the problem.  The framework previously generated it’s unique IDs to prevent ID collisions and the most common place for these types of collisions are inside databound controls.  Predictable mode is really designed to work with databound controls but does not have to.  There is three ways to uses the predictable mode, each one of these is defined through the ClientIDRowSuffix property that specifies the suffix for each instance.  The ClientIDRowSuffix uses values from the control’s datakeys collection, so if the control does not have a datakeys collection this property is not viable.  If this property is not set or is not available the row index will be used in it’s place.
1. With no ClientIDRowSuffix defined, this is also the behavior for databound controls without a datakeys collection e.g. Repeater Control.  Notice that the framework has traversed the control hierarchy and prefixed the ID with the parent’s ID and suffixed the ID with row index.
markup:
<asp:GridView ID="EmployeesNoSuffix" runat="server" AutoGenerateColumns="false" ClientIDMode="Predictable" >
    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="EmployeeID" runat="server" Text='<%# Eval("ID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="EmployeeName" runat="server" Text='<%# Eval("Name") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
output:
<table id="EmployeesNoSuffix" style="border-collapse: collapse" cellspacing="0" rules="all" border="1">
    <tbody>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">Name</th>
        </tr>
        <tr>
            <td><span id="EmployeesNoSuffix_EmployeeID_0">1</span></td>
            <td><span id="EmployeesNoSuffix_EmployeeName_0">EmployeeName1</span></td>
        </tr>
        ...
        <tr>
            <td><span id="EmployeesNoSuffix_EmployeeID_8">9</span></td>
            <td><span id="EmployeesNoSuffix_EmployeeName_8">EmployeeName9</span></td>
        </tr>
    </tbody>
</table>
2. With a ClientIDRowSuffix defined, this looks in the control’s datakeys collection for the value and then suffixes the ID with that value.
markup:
<asp:GridView ID="EmployeesSuffix" runat="server" AutoGenerateColumns="false" ClientIDMode="Predictable" ClientIDRowSuffix="ID" >
    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="EmployeeID" runat="server" Text='<%# Eval("ID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="EmployeeName" runat="server" Text='<%# Eval("Name") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
output:
<table id="EmployeesSuffix" style="border-collapse: collapse" cellspacing="0" rules="all" border="1">
    <tbody>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">Name</th>
        </tr>
        <tr>
            <td><span id="EmployeesSuffix_EmployeeID_1">1</span></td>
            <td><span id="EmployeesSuffix_EmployeeName_1">EmployeeName1</span></td>
        </tr>
        ...
        <tr>
            <td><span id="EmployeesSuffix_EmployeeID_9">9</span></td>
            <td><span id="EmployeesSuffix_EmployeeName_9">EmployeeName9</span></td>
        </tr>
    </tbody>
</table>
3. With a ClientIDRowSuffix defined, but instead of just one value a compound value will be used.  Exhibits the same behavior as one value but it will suffix both values onto the ID.
markup:
<asp:GridView ID="EmployeesCompSuffix" runat="server" AutoGenerateColumns="false" ClientIDMode="Predictable" ClientIDRowSuffix="ID, Name" >
    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="EmployeeID" runat="server" Text='<%# Eval("ID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="EmployeeName" runat="server" Text='<%# Eval("Name") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
output:
<table id="EmployeesCompSuffix" style="border-collapse: collapse" cellspacing="0" rules="all" border="1">
    <tbody>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">Name</th>
        </tr>
        <tr>
            <td><span id="EmployeesCompSuffix_EmployeeID_1_EmployeeName1">1</span></td>
            <td><span id="EmployeesCompSuffix_EmployeeName_1_EmployeeName1">EmployeeName1</span></td>
        </tr>
        ...
        <tr>
            <td><span id="EmployeesCompSuffix_EmployeeID_9_EmployeeName9">9</span></td>
            <td><span id="EmployeesCompSuffix_EmployeeName_9_EmployeeName9">EmployeeName9</span></td>
        </tr>
    </tbody>
</table>