Thursday, July 26, 2012

SharePoint 2010: Change the “Add New Item” (and other) messages for a web part

What we want : To change from “Add new item”:


JS Code :
<script>
var ABC = document.getElementsByTagName('XYZ');
var Counter = 0;
for (var j=0; j<ABC.length; j++)
{
  if (ABC[j].id == 'idHomePageNewItem')
  {
     Counter ++;
if (Counter == 2) { ABC[j].innerHTML='Click here to add team tasks' } } } </script>

Where should you add the code?

Add the code to the page using SharePoint Designer
  1. Edit the page using SharePoint Designer
  2. Find the “<asp:content” tag for PlaceHolderMain and then find the matching end tag (“</asp:content>”)
  3. Just before the end tag, add the JavaScript listed above
Or  add a Content Editor Web Part to the page.

ID’s used by web parts


Web Part Default message ID for “.getElementById” in the code below
Announcements Add new announcement “idHomePageNewAnnouncement” (changed in 2010)
Links Add new link "idHomePageNewLink" 
Calendar Add new event "idHomePageNewEvent"
Picture Library Add new picture “idHomePageNewItem”  *  (changed in 2010)
KPI List Add new item "idHomePageNewItem"  *  (changed in 2010)
Tasks Add new item "idHomePageNewItem"  *  (changed in 2010)
Project Tasks Add new item "idHomePageNewItem"  *  (changed in 2010)
Document Library Add new document "idHomePageNewDocument"  (changed in 2010)
Wiki Add new Wiki page "idHomePageNewWikiPage"   (changed in 2010)
Discussion Add new discussion "idHomePageNewDiscussion"  (changed in 2010)
Custom List Add new item “idHomePageNewItem” *   (changed in 2010)

Hide the “+” image?

If you want to get rid of the little icon just before the “New Item” text then just add one more line of code after where you change the message:      ABC[j].innerHTML='Click here to add team tasks';      ABC[j].previousSibling.previousSibling.style.display='none';

SharePoint Designer Validation Regular Expression


<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>

<script type="text/javascript">

$(document).ready(function() {

$("#aspnetForm").validate({
submitHandler: function(form) {
}});

$.validator.addMethod(
     "regex",
     function(value, element, regexp) {
         var check = false;
         var re = new RegExp(regexp);
         return this.optional(element) || re.test(value);
     },""
);

// VALIDATE TITLE
$("input[title='Title']").rules("add",{
required: true,
regex:"^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$",
messages: {
regex: "<br />Enter valid email address."
}
});

// BLUR -  Set focus after alert
$("input[title='Title']").blur(function() {
if (!$("input[title='Title']").valid()) {
this.focus();
}
});

// SAVE - Cannot save if regex rules adds
$("input[value='Save']").focus(function(){
if (!$("input[title='Title']").valid()) {
alert("You cannot save this Item..");
$("input[title='Title']").focus();
}
});

});

</script>

File Extention Validation Document Library Event Receiver


   protected string[] ValidExtensions = new string[] { "png", "jpeg", "gif" };

        public override void ItemAdding(SPItemEventProperties properties)
        {
            string strFileExtension = Path.GetExtension(properties.AfterUrl);

            bool isValidExtension = false;

            string strValidFileTypes = string.Empty;

            using (SPWeb web = properties.OpenWeb())
            {
                foreach (string strValidExt in ValidExtensions)
                {
                    if (strFileExtension.ToLower().EndsWith(strValidExt.ToLower()))
                    {
                        isValidExtension = true;
                    }
                    strValidFileTypes += (string.IsNullOrEmpty(strValidFileTypes) ? "" : ", ") + strValidExt;
                }

                // Here i am going to check is this validate or not if not than redirect to the
                //Error Message Page.
                if (!isValidExtension)
                {
                    properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                    properties.RedirectUrl = properties.WebUrl + "/_layouts/error.aspx?ErrorText=" + "Only " + strValidFileTypes + " extenstions are allowed";

                }
            }
            //base.ItemAdding(properties);
        }

Wednesday, July 25, 2012

SharePoint 2010 : Icons

SharePoint 2010 branding. I was trying to find the icon used as the Add (+ sign) in the SharePoint 2010 Site Title

<img src="/_layouts/images/fgimg.png" alt=":" style="height:10px;width:10px;position:relative;display:inline-block;overflow:hidden;" />

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

According to position we can set the Icon, also if we have to add our own logo or any image then just insert image in given path. By this way we can set the Icon in SharePoint 2010.


Sample Image that contains (+) image

What is SharePoint ?

"SharePoint" is shorthand that some people use to refer to one or more Microsoft SharePoint products or technologies. Your people can use SharePoint to set up collaborative sites to share information with others, manage documents from start to finish, and publish reports to help everyone make better decisions.


   SharePoint products -:


  • SharePoint Foundation    The underlying technology for all SharePoint sites. SharePoint Foundation is available for free on-premises deployment and was called Windows SharePoint Services in previous versions. You can use SharePoint Foundation to quickly create many types of sites where you can collaborate on Web pages, documents, lists, calendars, and data.
  • SharePoint Server    A server product that relies on SharePoint Foundation technology to provide a consistent, familiar framework for lists and libraries, site administration, and site customization. SharePoint Server includes all the features of SharePoint Foundation plus additional features and capabilities such as Enterprise Content Management, business intelligence, enterprise search, and personal profiles through My Sites.

Site Collection Locks in SharePoint

Locks can be Set/Reset in 4 ways:
  1. Using Web User Interface - Through Central Administration
  2. Using STSADM 
  3. Using PowerShell
  4. Using Object Model
    1. By .Net code
    2. By PowerShell
Using Web User Interface: 
Where to Look? Go to Central Administration > Application Management > Configure Quotas and Locks >Select Site collection >Select lock status option

STSADM:

To set Lock:
stsadm -o setsitelock -url <Site-collection-url> -lock <Lock-Type>

To Check current locks applied:
stsadm -o getsitelock -url <Site-collection-url>

Where <Lock-Type> can take one of the following value: 
  • none  - Removes all the locks
  • noadditions  - Prevents from Addition
  • readonly - Can't add/Update/Delete content
  • noaccess - You can't view the site at all
PowerShell:
Set-SPSite -Identity <Site-collection-url> -LockState <Lock-State>

Where: <Lock-State> can be:
  •     ReadOnly
  •     Unlock
  •     NoAdditions
  •     NoAccess 
Ex. 
$site = Get-SPSite -Identity <site url>
$site.readonly = $false 

Object Model:
You can Programmatically set the Lock status using SPSite object's Properties:
  •     SPSite.ReadLocked
  •     SPSite.WriteLocked
  •     SPSite.ReadOnly
Code Sample :

    using (SPSite site = new SPSite("http://spserver2:3030"))
                {
                    using (SPWeb sharePointWeb = site.RootWeb)
                    {
                        site.AllowUnsafeUpdates = true;
                        sharePointWeb.AllowUnsafeUpdates = true;

                     
                        site.ReadOnly = true;
                        site.LockIssue = "Tesing Lock";

                        site.AllowUnsafeUpdates = false;
                        sharePointWeb.AllowUnsafeUpdates = false;
                     }
                 }

Tuesday, July 24, 2012

How to Backup & Restore SharePoint Sites using PowerShell


  1. On the Start menu, click All Programs.
  2. Click Microsoft SharePoint 2010 Products.
  3. Click SharePoint 2010 Management Shell.


Color Change Task Status Vice in SharePoint 2010


Color change Task Status vice using SharePoint Designer 2010


<script type="text/javascript" src="../../Style Library/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $Text = $("td .ms-vb2:contains('Not Started')");
 $Text.parent().css("background-color", "#461B7E");
 var myelement = $Text.parent().parent();
 $Text = $("td .ms-vb2:contains('Completed')");
 $Text.parent().css("background-color", "#4CC417");
 $Text = $("td .ms-vb2:contains('In Progress')");
 $Text.parent().css("background-color", "#EAC117");
});
</script>