Monday, 28 April 2014

Hide the Content Type Column in SharePoint 2013

There are many good reasons to use Content Types… if you can get people to use them, and then leave them alone.   :-)

When creating a new list item based on a Content Type the user needs to select the Content Type from the New dropdown. Once selected, the Content Type should generally not be changed. Changing it is too easy as the user is offered the choice every time they edit the item.

Preventing this is pretty easy as all we need to do is hide the first row of the edit form's HTML table. The following JavaScript will do this for us and will work in both SharePoint 2007, SharePoint 2010 and SharePoint Online / Office 365:

<script>

var ttnTables = document.getElementsByTagName("TABLE")
for (var i=0;i<ttnTables.length;i++)
{
  if (ttnTables[i].className=="ms-formtable")
  {
    ttnTables[i].rows[0].style.display="none";
  }
}
</script>


Now the Content Type is hidden on the edit form.

But Site Owner Can Change it?
The only problem with this solution is that it also prevents the site owner from changing the Content Types. We need to only run the above code for non site owners. To do this we will use the SPSecurityTrimmedControl and set the PermissionString to a suitable value, such as ManageWeb.
<!-- default value -->
<script type="text/javascript">
  var canChangeContentType = false;
</script>

<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ManageWeb">
  <!--text for users who have the ManageWeb permission -->
  <script type="text/javascript">
    canChangeContentType = true;
  </script>
</SharePoint:SPSecurityTrimmedControl>

<script type="text/javascript">
if (canChangeContentType == false)
{
  var ttnTables = document.getElementsByTagName("TABLE")
  for (var i=0;i<ttnTables.length;i++)
  {
    if (ttnTables[i].className=="ms-formtable")
    {
      ttnTables[i].rows[0].style.display="none";
    }
  }
}
</script>

There is Work around
A user with permissions to create private views can create a DataSheet view with the Content Type column displayed and then edit the Content Type.

Where to Add this Code
SharePoint 2013
1.             Open SharePoint Designer and open the site with the list that uses Content Types
2.             In the Folder List pane expand Lists and your list
3.             Double-click EditForm.aspx
4.             If the HTML is not displayed, click the Code button at the bottom of the page
5.             Search for "PlaceHolderMain"
6.             Select the entire line, right-click the line and select Find Matching Tag (this should find </asp:Content>)
7.             Add the JavaScript from above just before the </asp:Content> line
8.             Save and test (remember, as a Site Owner you will always be able to see the Content Type)
SharePoint 2010
1.             Open SharePoint Designer and open the site with the list that uses Content Types
2.             In the Navigation Pane click Lists and Libraries
3.             Click the name of the list that uses Content Types
4.             In the Forms area click EditForm.aspx
5.             If the window is split into Design and Code views, click the Split button at the bottom of the window
6.             Click in the area just below the existing web part
7.             You should see something similar to the following in the Code view:
8.             Paste the JavaScript from above between the DIV tags
9.             Save and test (remember, as a Site Owner you will always be able to see the Content Type)

Saturday, 12 April 2014

Install\Deploying .wsp file in SharePoint2013 Central administration.


   1)    Run the below command on SharePoint 2010 Management Shell.

   Add-SPSolution -LiteralPath "path to wsp file\EventReceiverProject.wsp"
   Example: Add-SPSolution -LiteralPath "C:\EventReceiverProject.wsp"

    2)      On the Central Administration Home page, click System Settings.

    3)      In the Farm Management section, click Manage farm solutions.

    4)      On the Solution Management page, click the solution that you want to deploy. 

    5)      On the Solution Properties page, click Deploy Solution

    6)      On the Deploy Solution page, in the Deploy When section, select one of the following:

           a)      Now

           b)      At a specified time. If you select this option, specify a time by using the date and time boxes we recommend that you select a time when the load on the destination servers is low.

    7)      In the Deploy To? Section, in the A specific web application list, click either All web applications or select a specific Web application.

    8)      Click OK.

    9)      Now open your site, go to site settings ->Site features

   10)      Activate the feature if it is not active.

Here’s a quick overview of what we’ve learned with a few more commands you’ll probably need along the way:
·         Add Solution
Add-SPSolution c:<path><solution filename>.wsp
·         Deploy/Install Solution
Install-SPSolution –Identity <solution file>.wsp –WebApplication http://<url> -GACDeployment
·         Update Solution
Update-SPSolution –Identity <solution file>.wsp –LiteralPath c:<path><solution file>.wsp –GACDeployment
·         Uninstall Solution
Uninstall-SPSolution –Identity <solution file>.wsp –WebApplication http://<url>
·         Remove Solution
Remove-SPSolution –Identity <solution file>.wsp


Saturday, 1 March 2014

Transaction Sql Server Using Asp.net


 Transaction Sql Server Using Asp.net
       
First of all create a Sql Server Database e.g. "dbEmp" and create a table "EmpPersonalDetail" for storing the employee's personal details like Name, age and address using the script below:


CREATE TABLE [dbo].[EmpPersonalDetail]
(
    [EmpPersonalDetailIdPk] [int] IDENTITY(1,1) NOT NULL,
    [EmpName] [varchar](100) NULL,
    [Age] [int] NULL,
    [Address] [varchar](500) NULL
)

Create another table "EmpOfficeDetail" for storing the employee's official details like his department Name, his designation and the salary using the script below.


CREATE TABLE [dbo].[EmpOfficeDetail]
(
   [EmpOfficeDetailId] [int] IDENTITY (1,1) NOT NULL,
   [EmpPersonalDetailIdFk] [int] NULL,
   [DeptName] [varchar](100) NULL,
   [Designation] [varchar](100) NULL,
   [Salary] [decimal](18, 2) NULL
)

Also create Stored Procedure

CREATE PROCEDURE [dbo].[InsertEmpDetails_Sp]
            @EmpName VARCHAR(100),
            @Age     INT,
            @Address VARCHAR(100),
            @DeptName VARCHAR(100),
            @Designation VARCHAR(100),
            @Salary      DECIMAL(18,2)             
AS
BEGIN
 BEGIN TRANSACTION                               
   INSERT INTO EmpPersonalDetail(EmpName,Age,[Address])
    VALUES(@EmpName,@Age,@Address)                              
        IF (@@ERROR <> 0) GOTO ERR_HANDLER
        DECLARE @Id int
        --get the latest inserted id from the EmpPersonalDetail table                 
         SET @id= scope_identity()
                                
            INSERT INTO EmpOfficeDetail(DeptName,Designation,Salary,EmpPersonalDetailIdFk)
                         VALUES(@DeptName,@Designation,@Salary,@id)
                IF (@@ERROR <> 0) GOTO ERR_HANDLER                   
                COMMIT TRAN                                
                    RETURN 1
    GOTO AfterErrBlock

        ERR_HANDLER:                            
        ROLLBACK TRAN
                RETURN 0

        AfterErrBlock:
END


Above stored procedure first storing the employee's personal details like name, age and address in the "EmpPersonalDetail" table then checking the error status using @@ERROR. If it is 0 then next need to get the Id of the last inserted record?

So using scope identity() function id of the last inserted records is fetched from the "EmpPersonalDetail" table and then corresponding to that id, the official details are stored in the "EmpOfficeDetail" table and the changes are committed to the database using the COMMIT statement. If the @@ERROR status is not 0 then the control will be passed the Error handler where i have written the ROLLBACK statement that will abort the changes made to the database.

So if the transaction succeeded then this stored procedure will return 1 otherwise 0. I will check the return value of the stored procedure in the code behind and based on that display the success or failure message to the user.

Note: @@ERROR returns 0 if the last Transact-SQL statement executed successfully; if the statement generated an error, @@ERROR returns the error number.

Note: scope identity() returns the last identity value inserted into an identity column in the same scope.

Create an application for Transaction

.ascx

<form id="form1" runat="server">
 <div>
    <fieldset style="width:270px; color:greenyellow;">
    <legend class="auto-style1"><strong>Transaction Example</strong></legend>
     <table>
     <tr><td>Emp Name: </td><td>
         <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox></td></tr>
     <tr><td>Age: </td><td>
         <asp:TextBox ID="txtAge" runat="server"></asp:TextBox></td></tr>
     <tr><td>Address: </td><td>
         <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td></tr>
     <tr><td>Department: </td><td>
         <asp:TextBox ID="txtDept" runat="server"></asp:TextBox></td></tr>
     <tr><td>Designation: </td><td>
         <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox></td></tr>
     <tr><td>Salary: </td><td>
         <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox></td></tr>
          <tr><td>&nbsp;</td><td>
          <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
              <asp:Button ID="btnClear" runat="server" Text="Reset" OnClick="btnClear_Click" />
              </td></tr>
    </table>
    </fieldset>
    </div>
    </form>

Code Behind file in C#
.aspx.cs

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=jp-pc;Initial Catalog=jaya;Persist Security Info=True;User ID=sa;Password=learner");
        SqlCommand cmd = new SqlCommand("InsertEmpDetails_Sp", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@EmpName", txtEmpName.Text.Trim());
        cmd.Parameters.AddWithValue("@Age", Convert.ToInt32(txtAge.Text));
        cmd.Parameters.AddWithValue("@Address", txtAddress.Text.Trim());
        cmd.Parameters.AddWithValue("@DeptName", txtDept.Text.Trim());
        cmd.Parameters.AddWithValue("@Designation", txtDesignation.Text.Trim());
        cmd.Parameters.AddWithValue("@Salary", Convert.ToDecimal(txtSalary.Text));

        SqlParameter returnParameter = cmd.Parameters.Add("RetVal", SqlDbType.Int);
        returnParameter.Direction = ParameterDirection.ReturnValue;
        con.Open();
        cmd.ExecuteNonQuery();
        int statusVal = Convert.ToInt32(returnParameter.Value);
        if (statusVal == 1)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Record saved successfully');", true);
            ClearControls();
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Record could not be saved');", true);
        }

    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ClearControls();

    }
    private void ClearControls()
    {
        txtEmpName.Text = string.Empty;
        txtAge.Text = string.Empty;
        txtAddress.Text = string.Empty;
        txtDept.Text = string.Empty;
        txtDesignation.Text = string.Empty;
        txtSalary.Text = string.Empty;

    }





The two Different Tables shown below



SharePoint tenant opt-out for modern lists is retiring in 2019

We're making some changes to how environments can opt out of modern lists in SharePoint. Starting April 1, 2019, we're going to be...