Wednesday, 29 May 2013

SharePoint: SPSecurity.RunWithElevatedPrivileges method

This is used to execute the specified method with Full Control rights even if the logged in user does not otherwise have Full Control.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    // Your code here...
});


Note:

1.The RunWithElevatedPrivileges method can not be used within a sandbox solution

2.The item that is actioned with elevated privileges will be executed under the web applications App pool account in IIS, which means that the modified user of the document/list item will most likely display as SYSTEM ACCOUNT.

3.If you did not want to execute the code as the App Pool account for any reason, you could alternatively use a specific user account to execute the code under by implementing the SPUserToken class.

SharePoint: CAML Query not working based on WHERE clause

When we write the CAML query in SharePoint development and if we put WHERE clause in query it still returns all records.


For example look at the below query, it works fine in U2U builder but returns all records if we use the same in code.


SPQuery query = new SPQuery();
query.Query = "<Query><Where></Where></Query>"


Here is the Fix:

Remove the "<Query>" Tag and rewrite it

SPQuery query = new SPQuery();
query.Query = "<Where></Where>"

Wednesday, 22 May 2013

SharePoint 2010: Site Collection backup and restore using PowerShell

$FileLocation= "C:\testSite.cmp"

Backup:

Backup-SPSite -Identity $SiteCollectionUrl -Path $FileLocation -Force

Restore:

Restore-SPSite -Identity $SiteCollectionUrl -Path $FileLocation -Force -Confirm:$false

SharePoint 2010: Create site collection using PowerShell script

This article is about how to create a site collection in SharePoint using PowerShell script

$WebApplicationURL="http://testsite"
$SiteCollectionName="Test Site"
$SiteCollectionURL="http://testsite/sites/test"
$SiteCollectionTemplate="STS#0"
$SiteCollectionLanguage="1033"
$SiteCollectionOwner="domain\loginname"
$ContentDatabaseName="TestDB"


1. Create a site collection with a separate content database

#Create a new content database
New-SPContentDatabase -Name $ContentDatabaseName -WebApplication $WebApplicationURL

# Create site collection using new content database
New-SPSite -URL $SiteCollectionURL -OwnerAlias $SiteCollectionOwner -Language $SiteCollectionLanguage -Template $SiteCollectionTemplate -Name $SiteCollectionName -ContentDatabase $ContentDatabaseName  

2. Create a site collection with the default content database

New-SPSite -URL $SiteCollectionURL -OwnerAlias $SiteCollectionOwner -Language $SiteCollectionLanguage -Template $SiteCollectionTemplate -Name $SiteCollectionName

Thats it !!!



SharePoint 2010: Managed MetaData Service backup and restore using PowerShell script

Backup:

$mmsApplication = Get-SPServiceApplication | ? {$_.DiplayName –eq $MMSApplicationName}
$mmsProxy = Get-SPServiceApplicationProxy | ? {$_.DisplayName –eq $MMSProxyName}
  
Export-SPMetadataWebServicePartitionData -Identity $mmsApplication.Id –ServiceProxy $mmsProxy –Path $FileLocation


Restore:

$mmsApplication = Get-SPServiceApplication | where {($_.DisplayName) –eq $MMSApplicationName}
$mmsProxy = Get-SPServiceApplicationProxy | ? {$_.DisplayName –eq $MMSProxyName}  
 

Import-SPMetadataWebServicePartitionData -Identity $mmsApplication.Id –ServiceProxy $mmsProxy –Path $FileLocation –OverwriteExisting

Done!!!


Wednesday, 1 May 2013

Set Default value to Person or Group field in SharePoint 2010 using JavaScript and JQuery

This article is about how to set default value to person or group field when new list form opens using JavsScript and JQuery.

It includes the following

1. Get the members of a SharePoint group using SPServices JQuery library
2. Set the value to Person or Group field using JavaScript

<script type="text/javascript" language="javascript" src="/Style%20Library/JS/jquery-1.8.3.min.js"></script>
<script type="text/javascript" language="javascript" src="/Style%20Library/JS/jquery.SPServices-0.7.2.min.js"></script>
<script type="text/javascript">

    $(document).ready(function() {
      var strHTMLGroupUsers = "";
        $().SPServices({        
        operation: "GetUserCollectionFromGroup",
        groupName: "Approvers",
        async: false,
        completefunc: function(xData, Status) {
        $(xData.responseXML).find("User").each(function() {
          strHTMLGroupUsers += $(this).attr("Name")+";";
        });
      }
        });
        //get the people pickers input div
        var pp = getPickerInputElement("Approvers");
        //set it to the current user if we've found it
        if(pp != null)
        pp.innerHTML = strHTMLGroupUsers;
        $("img[title='Check Names']").parent().click();

    });

function getPickerInputElement(fieldsInternalName)
{
    var result  = "";
    var divs = document.getElementsByTagName("DIV");
    for(var i=0; i < divs.length ; i++)
     {
       if(divs[i].id=="WebPartWPQ3")
        {
          var tds = divs[i].getElementsByTagName("TD");
          for(var j=0; j < tds.length; j++)
           {
             var cellHTML = tds[j].innerHTML;
             
             if(cellHTML.indexOf('FieldInternalName="' + fieldsInternalName + '"') >= 0)
             {
               var innerDivs = tds[j].getElementsByTagName("DIV");
               for(var k=0; k < innerDivs.length; k++)
               {
                 if(innerDivs[k].id.indexOf("UserField_upLevelDiv") > 0)
                 {
                   result = innerDivs[k];
                   break;
                 }}}
             }
           }
        }
   return result;
}



</script>
 

Tuesday, 2 April 2013

Permission Issues when executing the PowerShell scripts

When we face any permission related issues while executing the PowerShell scripts, we need to check the following to fix it.

1. Check Window PowerShell execution policy
   Get-Execution Policy

2. If the return value is "Restricted" and then set the value as "RemoteSigned"
   Set-Execution Policy "RemoteSigned"

3. In addition to the above, also need to ensure that UAC (User Access Control) is disabled.

That's it.

SharePoint 2013: Video file URL from Asset Library

When you are working with Asset library in SharePoint 2013. It is very important to note that how video files are stored and managed in tha...