Tuesday, October 2, 2012

Get all subsites under SharePoint site using ECMA script

In this post, I will explain how to get all subsites under SharePoint site using ECMA script

<script type="text/ecmascript">
var currentcontext = null;
var currentweb = null;
ExecuteOrDelayUntilScriptLoaded(GetSites, "sp.js");
function GetSites()
{
currentcontext = new SP.ClientContext.get_current();
if(currentcontext!=null && currentcontext!=undefined ){
currentweb = currentcontext.get_web();
this.subsites = currentweb.get_webs();
currentcontext.load(this.subsites);
currentcontext.executeQueryAsync(Function.createDelegate(this, this.OnSuccess),
Function.createDelegate(this, this.OnFailure));
}
}
function OnSuccess(sender, args) {
var subsite = '';
var Listsubsite= this.subsites.getEnumerator();
while (Listsubsite.moveNext())
{
var Site = Listsubsite.get_current();
subsite += '\nID: ' + Site.get_id() + '\nTitle: ' + Site.get_title()+'\n Created date:'+ Site.get_created();
}
alert(subsite);
}
function OnFailure(sender, args) {
alert('Error');
}
</script>

Get Top site collection using ECMA script

In this post, I will explain how to get top site collection using ECMA script

 <script type="text/ecmascript">
var currentcontext = null;
var currentsitecol = null;
ExecuteOrDelayUntilScriptLoaded(GetTopLevelSiteCollection, "sp.js");
function GetTopLevelSiteCollection()
{
currentcontext = new SP.ClientContext.get_current();
if(currentcontext!=null && currentcontext!=undefined ){
currentsitecol= currentcontext.get_site().get_rootWeb();
currentcontext.load(currentsitecol);
currentcontext.executeQueryAsync(Function.createDelegate(this, this.OnSuccess),
Function.createDelegate(this, this.OnFailure));
}
}
function OnSuccess(sender, args) {
var sitecol='';
sitecol+= '\nID: ' + currentsitecol.get_id() + '\nTitle: ' + currentsitecol.get_title()+'\n Created date:'+ currentsitecol.get_created();
alert(sitecol);
}
function OnFailure(sender, args) {
alert('Error');
}
</script>

Saturday, August 25, 2012

SharePoint: Copy document from one document library to file system programmatically

In this post I will explained how we can programmatically copy documents from one document library to file system.
Create a User interface as shown below and add the code snippet
protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SPSite oSite = SPContext.Current.Site;
                SPWeb oWeb = oSite.RootWeb;
                   
                CopyDocToFileSystem(oWeb, txtSourceLib.Text, txtLocation.Text);
                lblMsg.Text = " sucessfully copied";
                     
            }
            catch (Exception ex)
            { }
           
        }


        public void CopyDocToFileSystem(SPWeb oWeb, string sourceDocLib, string destFilePath)
        {
            SPFolder oSourceDocLibraryFolder = oWeb.GetFolder(sourceDocLib);

            SPFileCollection oFileCol = oSourceDocLibraryFolder.Files;
            foreach (SPFile oFile in oFileCol)
            {
                byte[] b = oFile.OpenBinary();
                FileStream oFileStream = new FileStream(destFilePath + "\\" + oFile.Name, FileMode.Create, FileAccess.ReadWrite);
                BinaryWriter oBinaryWriter = new BinaryWriter(oFileStream);
                oBinaryWriter.Write(b);
                oBinaryWriter.Close();
            }
        }



SharePoint: Copy document from one document library to another programmatically

In this post I will explained how we can programmatically copy documents from one document library to another.
Firstly create two document library.Then create a Visual studio solution with User Interface as shown below
Add the below code snippet
protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
              SPSite oSite = SPContext.Current.Site;
              SPWeb oWeb = oSite.RootWeb;
                   
              SPFileCollection oFileCol = oWeb.GetFolder(txtSourceLib.Text).Files;

              foreach (SPFile oFile in oFileCol)
              {
                oFile.CopyTo(txtTargetLib.Text + "/" + oFile.Name, true);
                }
lblMsg.Text = "sucessfully copied";
            }
            catch (Exception ex)
            { }
           
        }

Wednesday, August 15, 2012

Add webpart using Toolpaneview

In this post I will explain how we can add web part from toolpaneview.
Create a webpart page and append ?ToolPaneView=2 to the page URL. The result page is as shown in below screenshot

Sunday, July 29, 2012

Dynamically Apply/change SharePoint 2010 Header Image

In this post I will explained how we can dynamically apply/change SharePoint 2010 site Header Image. Create Image library, add image named “SiteHeaderImg.png” and below code in master page
<script type="text/javascript">
  
    function ChangeImage() {
        try {
     var URL_str = L_Menu_BaseUrl + '/ImageLibrary/SiteHeaderImg.png';
            
                document.getElementById("s4-titlerow").style.background = "background-image: url('" + URL_str + "') no-repeat";
                /* (Works in Firefox) */
  document.getElementById("s4-titlerow").style.background = "url('../ImageLibrary/SiteHeaderImg.png') no-repeat";
      
        }
        catch (e) {
            alert(e);
        }
    }
    _spBodyOnLoadFunctionNames.push("ChangeImage()");
 
</script>

Hosting SharePoint 2010 InfoPath form in web part

In this article I will explained how to add InfoPath form in SharePoint 2010 InfoPath form web part. Firstly we will create an  EmployeeDetails list
with following fields
 Now we will customize the InfoPath form as shown below
Now we will create SharePoint page and edit that page to insert InfoPath form web part on it