Sunday, March 11, 2012

How to Back Up While the Web Application Is Still Running


There are three steps to backing up a SharePoint Foundation site collection that is up and running. First, create a snapshot of a content database. Second, create an unattached database object out of the snapshot. Third, use the unattached database as the source of the backup instead of the live content database.


To back up all of the content databases in a Web application, your code can iterate through the SPWebApplication..::..ContentDatabases property and run the three-step process on all the content databases. In addition, of course, you can iterate through all the Web applications of the server farm to back up all the content databases in the server farm without making the Web applications read-only.


To Backup a Site Collection

  1. Get a reference to a content database.
  2. Call the CreateSnapshot() method of the content database's Snapshots collection.
  3. Pass the snapshot's ConnectionString property to the static CreateUnattachedContentDatabase(SqlConnectionStringBuilder) method.
  4. Call Backup(String, String, Boolean) and pass it the URL of a particular site collection.

    In the following example, the current site collection is the one that is backed up, but the URL of any other site collection in the same content database could have been passed to the Backup(String, String, Boolean) method.


    SPSite siteCol = SPContext.Current.Site;
    SPContentDatabase cDB = siteCol.ContentDatabase;
    
    SPDatabaseSnapshot snap = cDB.Snapshots.CreateSnapshot();
    
    SPContentDatabase unDB = SPContentDatabase.CreateUnattachedContentDatabase(snap.ConnectionString);
    
    unDB.Sites.Backup(siteCol.ServerRelativeUrl.Trim('/'), "\\Server\Backups\MySite.bak", true);
    





Monday, March 5, 2012

Deploy Silverlight Web Part in SharePoint 2010

Let see how to use Silverlight web part in SharePoint.

Please install Silverlight sdk if its not installed before you proceed.

First create an Empty SharePoint Project in visual studio.

Add new Project in this solution. Select "Silverlight Application" from the project templates and uncheck the "Host the Silverlight application in a new Web site".

Open the "MainPage.xaml" file and drag a data grid on to this. Set Autogenerate columns property of the grid to false

Create the columns you want to show in the grid like below:

<sdk:DataGridTextColumn  Binding="{Binding ID}" Header="ID"></sdk:DataGridTextColumn>
                        <sdk:DataGridTextColumn  Binding="{Binding Title}"

Header="Title"></sdk:DataGridTextColumn>


Open the code file of MainPage and create a class. This class will be used to set

columns in the grid.

 public class ListDataItems
        {
            public int ID { get; set; }
            public string Title { get; set; }
}

On the grid i have used "{Binding ID}" and "{Binding Title}" for the columns.

The columns of grid you will specify, that should also be in the proprery of the class as specifed in above class.


Next create a Observable collection of the above class as below:

 ObservableCollection<ListDataItems> _items ,namespace is

using System.Collections.ObjectModel;

Create ListItemCollection instance like ListItemCollection _genericList

create a method to get list items from list using Silvelight client context.private

void GetItems()
        {
            var clientContext = ClientContext.Current;
            var oList = clientContext.Web.Lists.GetByTitle("LookupList");
            var camlQuery = CamlQuery.CreateAllItemsQuery();
            _genericList = oList.GetItems(camlQuery); ;
         
            clientContext.Load(_genericList);
         
            clientContext.ExecuteQueryAsync(Pass, fail);
        }

public void Pass(object sender, ClientRequestSucceededEventArgs args)
        {


            Dispatcher.BeginInvoke(() => loaddata());


        }


 public void fail(object sender, ClientRequestFailedEventArgs args)
        {
//caught exception here if any occurs

        }

 void loaddata()
        {

            foreach (var Listitem in _genericList)
            {

                _items.Add(new ListDataItems()
                {
                    ID = Convert.ToInt32(Listitem["ID"]),
                    Title = Convert.ToString(Listitem["Title"]),
                 
                 
                });
            }
        }

Call Method on page load

 public MainPage()
        {
            InitializeComponent();
            _items = new ObservableCollection<ListDataItems>();


            yourgridid.ItemsSource = _items;

                     
            GetItems();

        }

No with all the above code you data grid containing list data is ready. Silverlight

creates a xap file that contains all your userinterface and code into one file afetr

successfulling building the project. Next step is to deploy this Silverlight

component(.xap) to SharePoint.

In the empty SharePoint project created earliar, add new item, select Module from the

templates give a name e.g.ListViewSilverlight.

Right click on the newly created module, select properties, select Project Output

Reference, click on the browse button, on the left hand side from the Members select

your Silverlight project, on the right hand side selcet Deployment Type as Element

File and click ok.

In the elements.xml file specify where you want to deploy this file in SharePoint

 <Module Name="ListViewSilverlight" Url="Style Library">
 
    <File Path="ListViewSilverlight\ListViewSilverlightWebPart.xap"

Url="SilverLightModule/ListViewSilverlightWebPart.xap"  Type="GhostableInLibrary"/>
  </Module>

Now build and deploy the Project.

Create any web part page. Select Silverlight web part from the available web parts

speciy the path of your .xap file from style library and click ok.

You will see the grid containig list data is appreared on the screen.