Blog

How to Open a SharePoint Website with the Help of C# Code

Posted by Rahul Kumar Saxena Blogs | SharePoint 2010 Dec 19, 2012
In this blog, I am going to show how we can open a SharePoint website with the help of C# code And how we can access list, folder, file etc of a website.

In this blog, I am going to show how we can open a SharePoint website with the help of C# code And how we can access list, folder, file etc of a website.

This is the code which help us to open a website :

using (SPSite site = new SPSite(<Your SharePoint Site URL>))
{
      using(SPWeb web = site.OpenWeb())
      {
         //Your Code...
      }

}

If you want to access list of a web site then use below code :

using (SPWeb web = SPContext.Current.Site.OpenWeb("Your SharePoint Site URL "))
        {
            string MylistName = "YourListName";
            var lists = web.Lists;
            foreach (SPList list in lists)
            {
                if (list.Title.Equals(MylistName))
                    output.Write("<b>This list exists</b>");
            }

        }

If you want to authenticate your SharePoint through C# code then use below code :

var mytoken = SPSite.RootWeb.EnsureUser("usernameToImpersonate").UserToken;
        using (SPSite site = new SPSite(mytoken, this.ListAddress))
        {
            using (SPWeb web = site.OpenWeb())
            {
                // now your code here will be executed as selected user
            }

        }

Backup/Restore of SharePoint 2010 site Collection 


For Backup

Backup-SPSite -Identity http://localhost:1000 "c:\backup\MySpSitefile.bak"

For Restore
Restore-SPSite -Identity http://localhost::2000 -Path "c:\backup\
MySpSitefile.bak" -force

post comment
     
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts