Tuesday, February 21, 2012

Programmatically applying themes


//Open site collection object
using (SPSite mySite = new SPSite("http://SiteUrl"))
{

// Open site
using (SPWeb oWeb= mySite .OpenWeb())
{
oWeb.ApplyTheme("<theme to be applied>");
oWeb.Update();
}
}

Sunday, February 5, 2012

Programmtically list all attachements in a List

public void fnListAttachments(string site, string listName)
{
    StringBuilder strAttachNames= new StringBuilder();
    using (SPSite oSite = new SPSite(site))
    {
        using (SPWeb oWeb = oSite.OpenWeb())
        {
            SPList oList = oWeb.Lists[listName];

            SPListItemCollection oListItems = oList.Items;
            foreach (SPListItem item in oListItems)
            {
                SPAttachmentCollection attachmentscol= item.Attachments;

                if (attachmentscol.Count <= 0) return string.Empty;

                foreach (var attachment in attachmentscol)
                {
                    strAttachNames.Append(attachment.ToString());
                }
            }
        }
    }
    //return strAttachNames.ToString();
}

Feature handler to save documents to physical folder

  SPSite site = new SPSite(properties.SiteId);
        using (SPWeb web = site .OpenWeb()) {

            string folderName = "";
            SPFolder cols = web.Folders[site.Url + "/TestDocumentLib"];

            SPList list = web.Lists["TestDocumentLib"];
            SPDocumentLibrary docLibrary = (SPDocumentLibrary)list;
            SPListItem listItem = docLibrary.GetItemById(properties.ListItemId);

            SPFile file1 = web.GetFile(listItem.UniqueId);
            string strUrl = file1.ServerRelativeUrl.ToString();
            string destinationFolderUrl = @"C:\Documents and Settings\Documents\";
            byte[] binfile = file1.OpenBinary();
            string strRootUrl = list.RootFolder.ServerRelativeUrl.ToString();


            int count = strUrl.Split('/').Length - 1;
            string[] str = strUrl.Split('/');

            for (int i = 1; i < count; i++)
            {
                destinationFolderUrl += str[i] + "\\" + folderName;
            }
            SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               if (!(Directory.Exists(destinationFolderUrl)))
               {

                   Directory.CreateDirectory(destinationFolderUrl);

                   FileStream fstream = System.IO.File.Create(destinationFolderUrl + "\\" + file1.Name);
                   fstream.Write(binfile, 0, binfile.Length);
                   fstream.Close();
               }
               else
               {
                   //Directory.CreateDirectory(destinationFolderUrl);
                   FileStream fstream = System.IO.File.Create(destinationFolderUrl + "\\" + file1.Name);
                   fstream.Write(binfile, 0, binfile.Length);
                   fstream.Close();
               }

           });
        }
}

A sales engineer is someone who promise you a bridge, even when there's no river.