Wednesday, May 25, 2011

How to export the documents from sharepoint document library to hard drive

Sample to export the documents from sharepoint document library to hard drive

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using Microsoft.SharePoint.Administration;
namespace ExportDocuments
{
    class Exportdoc
    {
        private static void Main()
        {
            string strDownloadPath = @"C:\MyDownloadFolder\";//physical location
            string url = "http://Site_Url";
            using (SPSite oSiteCollection = new SPSite(url))
            {
                SPWebCollection collWebsites = oSiteCollection.AllWebs;
                Console.WriteLine("Websites Count: {0}", collWebsites.Count);
                Console.WriteLine("  ");
                string rootwebtitle = oSiteCollection.RootWeb.Title;
                foreach (SPWeb oWebsite in collWebsites)
                {
                    Console.WriteLine("Site : " + oWebsite.Title);
                    //Path to download the files
                    string websiteurl = oWebsite.Url;
                    string framedUrl = websiteurl.Replace(oSiteCollection.Url, "");
                    string path = strDownloadPath + "\\" + rootwebtitle + framedUrl.Replace('/', '\\');
                    Directory.CreateDirectory(path);
                    DownloadDocs(oWebsite.Url, path);
                    oWebsite.Dispose();
                    Console.WriteLine("--------------------");
                }
            }
            Console.WriteLine("Please click enter to exit");
            Console.ReadLine();
        }
       private static void DownloadDocs(string siteURL, string sitepath)
        {
            //List of OOTB document libraries - These doc libraries will not be downloaded
            List<string> ootbLib = new List<string>();
            ootbLib.Add("Converted Forms");
            ootbLib.Add("Form Templates");
            ootbLib.Add("Images");
            ootbLib.Add("List Template Gallery");
            ootbLib.Add("Master Page Gallery");
            ootbLib.Add("Pages");
            ootbLib.Add("Reporting Templates");
            ootbLib.Add("Site Collection Documents");
            ootbLib.Add("Site Collection Images");
            ootbLib.Add("Site Template Gallery");
            ootbLib.Add("Style Library");
            ootbLib.Add("Web Part Gallery");
            using (SPSite oSite = new SPSite(siteURL))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    foreach (SPList list in oWeb.Lists)
                    {
                        string actpath = sitepath + "\\" + list.Title+ "\\"  ;
                        if (list.BaseType == SPBaseType.DocumentLibrary)
                        {
                            if (!ootbLib.Exists(delegate(string p) { return p.Trim() == list.Title; }))
                            {
                                Console.WriteLine("Downloading from Library ::" + list.Title);
                                Directory.CreateDirectory(actpath);
                                TraverseAllListItems(list,actpath);
                            }
                        }
                    }
                }
            }
        }
        private static void TraverseAllListItems(SPList list,string path)
        {
            SPQuery qry = new SPQuery();
            qry.ViewAttributes = "Scope=\"Recursive\"";
           
            try
            {
                
                SPListItemCollection ic = list.GetItems(qry);
                foreach (SPListItem subitem in ic)
                {
                    string actpath = path + "\\" + subitem.Url;
                   
                    string itemurl = subitem.Url;
                    int index = itemurl.IndexOf("/");
                    string subpath = itemurl.Substring(index + 1);
                    string finalpath = path + subpath;
                    finalpath = finalpath.Replace(subitem.Name, "");
                    finalpath = finalpath.Replace("/", "\\");
                    Console.WriteLine(finalpath);
                    Directory.CreateDirectory(finalpath);
                    byte[] binFile = subitem.File.OpenBinary();
                    System.IO.FileStream fstream = System.IO.File.Create(finalpath + "\\" + subitem.Name);
                    fstream.Write(binFile, 0, binFile.Length);
                 
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
}
}
 Happy coding :)

Thursday, March 3, 2011

How to get list items through Sharepoint Object Model

Programmatically we can get sharepoint list items through Object Model.
It is so easy.

Just we need SPSite, SPWeb instances and get the list out of it. Then we need to formulate SPQuery and get the items.

There you go.

using(SPSite site=new SPSite("Site_URL")
{
   using(SPWeb web=site.OpenWeb())
   {
    SPList list = web.Lists["List_Name"];
    SPQuery query = new SPQuery();
    query.ViewAttributes = "Scope=\"Recursive\"";// this will search your folders too
    SPListCollection newsListColl = list.GetItems(query);
    }
}

Friday, February 25, 2011

How to update User profile property using Object Model

User profiles are just repositories for information about individual people. We can programmatically update the properties of user profiles. You just need to specify the account you want to update the property.

Here is the fun part: the code :)

using(SPSite site = new SPSite(http://Site_URL/))
{
     using(SPWeb web = site.OpenWeb())
       {
            ServerContext context = ServerContext.GetContext(site);
         UserProfileManager profMgr = new UserProfileManager(context);
         string acc = @"domaniname\loginname";
         UserProfile profile = profMgr.GetUserProfile(acc);
         profile["AboutMe"].Value = (String.IsNullOrEmpty(txtPropValue.Text)) ? "property_value" : txtPropValue.Text;
         profile.Commit();
      }
}



Happy Coding!!!