Wednesday, March 4, 2015

List Active Databases used in sharepoint 2010 By Power Shell

Get List of All Databases
Goto SharePoint 2010 Management Shell

Get-SPDatabase | Sort-Objectdisksizerequired -desc | Format-Table Name

Get list of Content Databases

Get-SPContentDatabase | Sort-Object Name | Format-Table Name

Thursday, March 27, 2014

Event Receiver


SharePoint Item Event Receiver
public class EventReceiver1 : SPItemEventReceiver

    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);
           string workphone = properties.AfterProperties["WorkPhone"].ToString();
           string email = properties.AfterProperties["Email"].ToString();
           if (!string.IsNullOrEmpty(workphone))
           {
               if (!System.Text.RegularExpressions.Regex.IsMatch(workphone , @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$"))
               {
                   string sErrMsg = "Business Phone is not in correct format";
                   properties.ErrorMessage = sErrMsg;
                   //properties.Status = SPEventReceiverStatus.CancelWithError;
                   properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                   properties.RedirectUrl = string.Format("/_layouts/ListItemEventReceive/EventReceiverErrorPage.aspx?ErrMsg={0}",sErrMsg);
               }              
           }
           if (!string.IsNullOrEmpty(email))
           {
               if (!System.Text.RegularExpressions.Regex.IsMatch(email, @"^(?("")("".+?""@)|(([0-9a-zAZ]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zAZ]{2,6}))$"))
               {
                   string sErrMsg = "Email is not in correct format";
                   properties.ErrorMessage = sErrMsg;
                   //properties.Status = SPEventReceiverStatus.CancelWithError;
                   properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                   properties.RedirectUrl = string.Format("/_layouts/ListItemEventReceive/EventReceiverErrorPage.aspx?ErrMsg={0}", sErrMsg);
               }
           }
       }
    }

Application Page:

EventReceiverErrorPage.aspx

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EventReceiverErrorPage.aspx.cs" Inherits="ListItemEventReceive.Layouts.ListItemEventReceive.EventReceiverErrorPage" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:Label ID="lblErrMsg" runat="server" Text=""></asp:Label>
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Event Receiver Error
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
Event Receiver Error
</asp:Content>


EventReceiverErrorPage.aspx.cs

    public partial class EventReceiverErrorPage : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sErrMsg = Request.Params["ErrMsg"];
            lblErrMsg.Text = sErrMsg;
        }
    }



Friday, January 11, 2013

Visual Webpart "Hello" User / Hello World

Drag and Drop a Lable in Design

Code:


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint; 

namespace VisualWebPartProject2.VisualWebPart1

{

    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "<font color = blue'><marquee> Hello  "+ SPContext.Current.Web.CurrentUser.Name +"</marquee></font>";
        }
    }
}

 

 

Visual Webpart for Creating Sharepoint Sites

UI:
<div>
    <asp:Label ID="lblSiteName" Text="Site Name: " runat="server" Width="100" />
    <asp:TextBox ID="txtSiteName" runat="server" />

    <asp:Label ID="lblSiteDescription" Text="Site Description: " runat="server" />
    <asp:TextBox ID="txtSiteDescription" runat="server" />

    <asp:Label ID="lblIntendedURL" Text="Intended URL: " runat="server" />
    <asp:TextBox ID="txtIntendedURL" runat="server" />

    <asp:Label ID="lblSiteTemplate" Text="Site Template: " runat="server" />
    <asp:DropDownList ID="ddlSiteTemplate" runat="server"
        onselectedindexchanged="ddlSiteTemplate_SelectedIndexChanged">
    </asp:DropDownList>

    <asp:CheckBox ID="chkUniquePermissions" Text="Unique Permissions " runat="server" />
    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
        Text="Submit" />

</div>

Code:


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using Microsoft.SharePoint;

namespace Jan10thVWP1.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        SPWebTemplateCollection wt;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtSiteName.Text == string.Empty && txtIntendedURL.Text == string.Empty)
                return;

            using (SPSite site = new SPSite(@"http://sp2010"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    try
                    {

                        SPSecurity.RunWithElevatedPrivileges(() => web.Webs.Add(
                            txtIntendedURL.Text,
                            txtSiteName.Text,
                            txtSiteDescription.Text,
                            1033,
                            ddlSiteTemplate.SelectedValue,
                            chkUniquePermissions.Checked,
                            false));

                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "alert('Success');", true);
                    }
                    catch (Exception ex)
                    {
                        string error = ex.Message;
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "alert('" + error + "');", true);

                    }
                    finally
                    {
                        txtSiteName.Text = string.Empty;
                        txtSiteDescription.Text = string.Empty;
                        txtIntendedURL.Text = string.Empty;
                    }
                }
            }

        }
        protected override void OnLoad(EventArgs e)
        {

            EnsureChildControls();
            if (!IsPostBack)
            {
                using (SPSite site = new SPSite(@"
http://sp2010"))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        try
                        {
                            wt = web.GetAvailableWebTemplates(1033);
                            foreach (SPWebTemplate template in wt)
                            {
                                ddlSiteTemplate.Items.Add(new ListItem(template.Title, template.Name));
                            }
                            ddlSiteTemplate.DataBind();

                        }
                        catch (Exception ex)
                        {
                            string error = ex.Message;
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "alert('" + error + "');", true);
                        }
                    }
                }
            }

        }
        protected void ddlSiteTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

Thursday, January 3, 2013

Updating Title of a SharePoint Site / Subsite using SharePoint Object Model

File | New | Project | Visual C# | .Net Frame Work 3.5 | Console Application

Give a Name there: | Choose Location | Click on Ok.

1. Add reference : Microsoft.SharePoint.dll
from the location:
c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.dll

2. Right Click on Console Application Project |
From Application | Target Framework | .Net Framework 3.5
Build | Target Plat from | Either X64 or Any Cpu | Save the changes

Add u r reference :
using Microsoft.SharePoint.dll;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using( SPSite  spSite = new SPSite("http://sp2010:8081") )
            {
                using (SPWeb spWeb = spSite.RootWeb)
                { 
                    foreach (SPWeb subWeb in spWeb.Webs)
                    {
                        subWeb.Title = spWeb.Title + subWeb.Title;
                        subWeb.Update();
                    }
                    spWeb.Title = "Hello" + spWeb.Title;
                    spWeb.Update();
                }
            }
        }
    }
}

 

 

Reading Titles of all the Sub Sites of a SharePoint site collection

File | New | Project | Visual C# | .Net Frame Work 3.5 | Console Application

Give a Name there: | Choose Location | Click on Ok.

1. Add reference : Microsoft.SharePoint.dll
from the location:
c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.dll

2. Right Click on Console Application Project |
From Application | Target Framework | .Net Framework 3.5
Build | Target Plat from | Either X64 or Any Cpu | Save the changes

Add u r reference :
using Microsoft.SharePoint.dll;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using( SPSite  spSite = new SPSite("http://sp2010:8081") )
            {
                using (SPWeb spWeb = spSite.RootWeb)
                {
                    foreach (SPWeb subWeb in spWeb.Webs)
                    {
                        Console.WriteLine(subWeb.Title);
                        Console.ReadLine();
                    }
                }
            }
        }
    }
}
 

Reading a SharePoint Site title Using a console App

File | New | Project |  Visual C# |  .Net Frame Work 3.5  | Console Application
 
Give a Name there:  | Choose Location |  Click on Ok.
 
1. Add reference : Microsoft.SharePoint.dll
      from the location:
            c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.dll
 
2.  Right Click on Console Application Project |
From Application |           Target Framework   | .Net Framework 3.5 
             Build | Target Plat from | Either X64 or Any Cpu  | Save the changes
 
Add u r reference :
using Microsoft.SharePoint.dll;


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using( SPSite  spSite = new SPSite("http://sp2010") )
            {
                using (SPWeb spWeb = spSite.RootWeb)
                {
                    Console.Write(spWeb.Title);
                    Console.ReadLine();
                }
            }
        }
    }
}