Showing posts with label sharepoint library. Show all posts
Showing posts with label sharepoint library. Show all posts

Monday, October 6, 2014

Deploying Lists and Adding new lists via features

Hi Readers,

This post will give you information on deploying lists , making changes to the lists and adding new lists to the Sharepoint site in O365 or on premise.

What we are using here is feature upgrades, its simple and fast also Sharepoint manages most of the work.

Lets go step by step

First create a project and add a list

Add a new column and name it description



Now from the solution explorer open "Feature1" this will be the feature which we'll be using to deploy and update our lists


 Now press F4 or open the properties pane and give a version to the feature, as its the first deployment I've put version 1.0.0.0

Now package the WSP and upload it the the solution gallery, you can get to solution gallery by going to "Site settings -> Solutions" , and activate the solution.

Now go to site features from site settings and you can view the feature that we have deployed, activate it.



Now our list will be created.


As we have deployed the list, lets see how we can upgrade it, for this I'm going to add a new column to the list,that is called SubTitle

Now open the feature1 again and give it a new version number "2.0.0.0"

Providing a new version number will not update it, we'll have to edit the feature's manifest to do this,
First open feature1 and move to the manifest tab.

 At the bottom you'll see a link saying "Overwrite generated xml file", now you'll see the screen below click the "Edit manifest in the xml editor"

Click that and you'll see the manifest of the feature, the manifest will look like this

Now we'll have to edit the manifest and add upgrade actions to it, here we are asking sharepoint to apply the element manifest if the version is greater than 2.0.0.0.


Now package the WSP.
Here we'll have to do another step before deploying , that is rename the file, then only sharepoint will ask to upgrade it.

Upload the new file and it'll ask you to upgrade the solution. click upgrade.

 Now you can see that the newer version is activated.


 If you go to your list it'll be updated and the new column will be there.


Now we'll see how to add a new library to our feature.
Add a new list and select the type as library


Now you'll see that a new feature is added to your solution, you may delete it, because its not required.


Open the feature again but this time we'll need to go the edit manifest screen and copy the upgrade actions we did in the last part,
Keep it safe in a notepad.

And click the "Discard manifest edits..." and now you can add the new library to the feature using the UI.

Again go to manifest tab and go to XML editor , here we'll have to update the version number and add new upgrade actions, you can see the edits that are done to the xml file. Also the version is increased as well.


You may create the wsp and upgrade the solution but the list will not be created.


If you increase the version again and deploy n upgrade the solution , the list will be created.


You can see that the latest version is activated.

And now your library is created.
You can use this technique to update lists and add new lists, any site that is having this feature will be upgraded and the new lists will be created.

Happy Coding
-Guruparan Giritharan-

Tuesday, February 5, 2013

Download files from Sharepoint Library Recursively

Hi
This is a small program that will download the contents of a Sharepoint documnt library.

This will download all the files in the subfolders as well

class Program
{
 static void Main(string[] args)
 {
  Console.WriteLine("This Application will download the documents in a library");
  Console.Write("Enter Site URL : ");
  string SiteURL = Console.ReadLine();

  Console.Write("Library Name : ");
  string LibraryName = Console.ReadLine();

  //Open the site using the given url
  using (SPSite site = new SPSite(SiteURL))
  {
   //Open the web application
   using (SPWeb web = site.OpenWeb())
   {
    try
    {
     //Get the library folder
     SPFolder folder = web.GetFolder(LibraryName);

     //Download files
     downloadFilesrecursively(folder);
    }
    catch (Exception ex)
    {
     //Print the details of the exception
     Console.WriteLine("An Error Occured while processing " + ex.Message);
    }
   }
  }

  Console.ReadLine();
 }

 //The method that downloads the files from a given SPFolder
 public static void downloadFiles(SPFolder folder)
 {
  foreach (SPFile item in folder.Files)
  {
   //Download the file
   byte[] fileData = item.OpenBinary();
   System.IO.FileStream fstream = System.IO.File.Create(item.Name);
   fstream.Write(fileData, 0, fileData.Length);
   Console.WriteLine(item.Name + " " + fileData.Length);
  }
 }

 //Method that is used to call the download method recursively
 public static void downloadFilesrecursively(SPFolder folder)
 {
  //If the folder have subfolders call the download
  //Methods for the subfolders
  if (folder.SubFolders.Count != 0)
  {
   //Download the files in the current folder
   downloadFiles(folder);
   foreach (SPFolder item in folder.SubFolders)
   {
    downloadFilesrecursively(item);
   }
  }
  else
  {
   //If the folder has no subfolders
   //then download the file in the folder
     downloadFiles(folder);   
  }
 }
}