none
Accessing Documents in Sharepoint programmatically RRS feed

  • Question

  • I'm new to sharepont and am trying to figure out how to access and edit documents programatically.  I want to make a program that will let all users with access to the server do this, not just the administrator.  Anyone know what I should do?
    • Edited by Mike Walsh FIN Wednesday, July 22, 2009 4:34 PM programmatically added to Title
    Wednesday, July 22, 2009 4:24 PM

Answers

  • Hi,

    I think this code achieves what you need - it opens a file hosted in SharePoint, modifies and saves.

    Please note: it assumes the document is a simple text file. If you want to modify Excel, you need to take Excel-specific actions. But then again, this should give you a good idea.

    Answering further... If your goal is to synchronize your Excel files stored on SharePoint (say, on daily basis) with some other data, you may want to create a Class Library which does the work (based on a code similar to the one below), then add it as a SharePoint timer job. But first, create it as a Console Application and make sure your code works. Then convert to a Timer job class library.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    
    namespace TestApp
    {
        class Program
        {
            public static SPWeb Web;
            public const string LIST_NAME = "List Name Goes Here";
    
            static void Main(string[] args)
            {
                SPSite site = new SPSite("http://HOSTNAME:PORT/");
    
                Web = site.RootWeb;
    
                SPListItemCollection items = Web.Lists[LIST_NAME].Items;
    
                foreach (SPListItem item in items)
                {
                    SPFile file = item.File;
    
                    file.CheckOut();
    
                    byte[] byteText = file.OpenBinary();
                    string text = Encoding.UTF8.GetString(byteText, 0, byteText.Length);
                    text += " More changes!!!";
                    byte[] modifiedText = Encoding.UTF8.GetBytes(text);
    
                    file.SaveBinary(modifiedText);
    
                    file.CheckIn("Automatic modification");
                }
            }
        }
    }
    


    Regards,
    Hristo Yankov

    My blog - http://blog.myitechnology.com

    (Was my post helpful? If so, please indicate)
    • Edited by Hristo Yankov Wednesday, July 22, 2009 4:50 PM
    • Marked as answer by Viplav Wednesday, July 22, 2009 6:12 PM
    • Unmarked as answer by Viplav Wednesday, July 22, 2009 6:43 PM
    • Marked as answer by Viplav Wednesday, July 22, 2009 8:30 PM
    Wednesday, July 22, 2009 4:46 PM

All replies

  • You need to give more details?  This is to vague to be able to give you information. 
    Wednesday, July 22, 2009 4:29 PM
  • Hi,

    You can access the document library file by using SPListItem.File property. After modifying it, you can save it back to its original location. As for the users - the user credentials of the current user running your program should be passed to SharePoint. Just make sure you add the users you need in the Site permissions.

    Regards,
    Hristo Yankov

    My blog - http://blog.myitechnology.com

    (Was my post helpful? If so, please indicate)
    Wednesday, July 22, 2009 4:30 PM
  • SharePoint has lists and document libraries.  Document libraries contain documents, each document has meta data which is stored against a list item.  The list item is represented by the SPListItem class, this has a property called File which gives you access to the SPFile class that represents your document.

    How will users edit these documents?  Will this be part of some business process?


    My SharePoint Blog - http://www.davehunter.co.uk/blog
    Wednesday, July 22, 2009 4:31 PM
  • Let's say I create a folder which contains excel files which contain outdated data.  I need to make a program to edit the file.

    Wednesday, July 22, 2009 4:35 PM
  • Hi,

    I think this code achieves what you need - it opens a file hosted in SharePoint, modifies and saves.

    Please note: it assumes the document is a simple text file. If you want to modify Excel, you need to take Excel-specific actions. But then again, this should give you a good idea.

    Answering further... If your goal is to synchronize your Excel files stored on SharePoint (say, on daily basis) with some other data, you may want to create a Class Library which does the work (based on a code similar to the one below), then add it as a SharePoint timer job. But first, create it as a Console Application and make sure your code works. Then convert to a Timer job class library.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    
    namespace TestApp
    {
        class Program
        {
            public static SPWeb Web;
            public const string LIST_NAME = "List Name Goes Here";
    
            static void Main(string[] args)
            {
                SPSite site = new SPSite("http://HOSTNAME:PORT/");
    
                Web = site.RootWeb;
    
                SPListItemCollection items = Web.Lists[LIST_NAME].Items;
    
                foreach (SPListItem item in items)
                {
                    SPFile file = item.File;
    
                    file.CheckOut();
    
                    byte[] byteText = file.OpenBinary();
                    string text = Encoding.UTF8.GetString(byteText, 0, byteText.Length);
                    text += " More changes!!!";
                    byte[] modifiedText = Encoding.UTF8.GetBytes(text);
    
                    file.SaveBinary(modifiedText);
    
                    file.CheckIn("Automatic modification");
                }
            }
        }
    }
    


    Regards,
    Hristo Yankov

    My blog - http://blog.myitechnology.com

    (Was my post helpful? If so, please indicate)
    • Edited by Hristo Yankov Wednesday, July 22, 2009 4:50 PM
    • Marked as answer by Viplav Wednesday, July 22, 2009 6:12 PM
    • Unmarked as answer by Viplav Wednesday, July 22, 2009 6:43 PM
    • Marked as answer by Viplav Wednesday, July 22, 2009 8:30 PM
    Wednesday, July 22, 2009 4:46 PM
  • SPList does not work,  is there any other way to access files?
    Wednesday, July 22, 2009 6:44 PM
  • Please explain how it doesn't work exactly? I tested it in a real environment ok. Can you please provide error?

    Regards,
    Hristo Yankov

    My blog - http://blog.myitechnology.com

    (Was my post helpful? If so, please indicate)
    Wednesday, July 22, 2009 7:03 PM
  • All my files are documents and are not stored as lists.  When i run the code:
            public static SPWeb Web;
            public const string LIST_NAME = "abcd";
    
            static void Main(string[] args)
            {
                SPSite site = new SPSite("http://moss-server/Docs/VIPLAV");
    
                Web = site.RootWeb;
                foreach (SPList list in Web.Lists)
                    Console.WriteLine(list.Title);
    none of my documents show up.
    Wednesday, July 22, 2009 7:10 PM
  • So you don't have list items in the list 'abcd'?

    Regards,
    Hristo Yankov

    My blog - http://blog.myitechnology.com

    (Was my post helpful? If so, please indicate)
    Wednesday, July 22, 2009 7:18 PM
  • yeah i cant seem to find any of my documents
    Wednesday, July 22, 2009 8:10 PM
  • try this: by default your documents are stored in document library - which in turn is also a list in Sp terms. find out that document lib name and replace 'abcd; with that name.

    using (SPSite site = new SPSite("http://SharePointServer/mySite")) // if you have your documents stored under root site remove mySite in this line
                //site.
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList myLists = web.Lists["abcd"];
                        foreach (SPListItem item in myLists.Items)
                        {
                          Console.WriteLine(item.Title);                       
                        }
    Console.ReadLine();
                    }
                }
    • Edited by Pandian.S Wednesday, July 22, 2009 8:30 PM
    Wednesday, July 22, 2009 8:27 PM
  • sorry, figured it out, Hristo's code worked, I was just using the wrong listname.

    thanks for all your help guys.
    Wednesday, July 22, 2009 8:30 PM