none
Open a file from a sharepoint list in C#

    Question

  • Hi there I was wondering if there are any API where if i provoide the API a ListId and ItemId that it would allow me to open/download the file?

    Basically currently we have a web part that pulls all new/modified items (could be from a document library, discussion forum, etc..) and I like to be able to modify this web part to open documents for opening or saving when clicked.

    If not could some one direct me to the right direction in how I would be able to accomplish this?

    Thanks in advance.

    Tuesday, May 18, 2010 4:49 PM

Answers

  • OK, i can only assume then that you have access to the code that is rendering your list of content. It is most likely already using the Microsoft API to get the list of content - in which case you just need to locate where it is rendering the link to this "screen where you can open the doc, edit, delete, etc" and change it to point directly to the document itself rather than this "screen".

    As you asked earlier, if you have the ListId and ItemId then you would simply need to use something similar to this to get an actual item URL:

    SPList.GetItemById will return you an SPListItem object using the ItemId - you first need to get an SPList item which you can get from an SPWeb object (the code your webpart uses will most likely already have this available). The SPWeb object Lists collection can give you the SPList object. Once you have the SPListItem object then you can use it's Url property. If you replace the link to the "screen" with this link it will directly open the document or show the save dialog.

    Further API documentation on the SPListItem is available here http://msdn.microsoft.com/en-us/library/ms415089(v=office.12).aspx


    Read my wiki at www.intheknow.it for more code and tips for developing with SharePoint 2007 & 2010
    Twitter: @starznet

    Technical Architect at Starznet Ltd. WSS/MOSS development and customisation with a primary focus on CMS.
    • Marked as answer by Chochobeelai Tuesday, May 18, 2010 5:45 PM
    Tuesday, May 18, 2010 5:29 PM

All replies

  • If your webpart renders a hyperlink to the document, then clicking the link would automatically allow the item to be opened or saved, there is nothing additional required to have this functionality.


    Read my wiki at www.intheknow.it for more code and tips for developing with SharePoint 2007 & 2010
    Twitter: @starznet

    Technical Architect at Starznet Ltd. WSS/MOSS development and customisation with a primary focus on CMS.
    Tuesday, May 18, 2010 4:51 PM
  • We want to avoid the 2nd click to access documents... if that was the case then I would not be posting here now would I =)

     

    Tuesday, May 18, 2010 5:01 PM
  • What second click? there is only one click - on the document hyperlink
    Read my wiki at www.intheknow.it for more code and tips for developing with SharePoint 2007 & 2010
    Twitter: @starznet

    Technical Architect at Starznet Ltd. WSS/MOSS development and customisation with a primary focus on CMS.
    Tuesday, May 18, 2010 5:04 PM
  • The web part, "Smart part" or what ever it is called, displayed a list of content that has been added/modified, when you click on the link it brings you to another screen where you can open the doc, edit, delete, etc... this happens for all content and we want to avoid this for documents.

     

    Tuesday, May 18, 2010 5:09 PM
  • Is it definately the "Smart Part" webpart you are using - if so there would be a control hosted inside it - is this a custom control that you are able to modify - or something that has been written by a 3rd party that you cannot just open in Visual Studio and begin changing?

    If it is not the "Smart Part", then what webpart is it? Is it a custom one? Is there any more information you can give about what you are using to display this list of content.

    Unfortunately your question isn't as straightforward as you may think, my simple answer to your first question is YES there is, the API is in the Microsoft.SharePoint.dll and gives you full access to almost everything in a SharePoint site, you can use the SPSite and SPList objects to return a list instance, and then use an SPQuery to pull the items out of the list that you require...however, unless you can actually modify the webpart to use the API then it is not going to be much use to you. If the webpart is something you are not able to modify in this manner, then you may be looking at changing how thigns are rendered, this could be a simple case of changing an XSLT file (as most of the query webparts in MOSS use), or it could be a template in an ASCX file - if it is neither then you may be looking at a completely custom webpart - or use an existing one which requries some modification - until you can clarify the above information then nobody here will be able to advise you correctly :)


    Read my wiki at www.intheknow.it for more code and tips for developing with SharePoint 2007 & 2010
    Twitter: @starznet

    Technical Architect at Starznet Ltd. WSS/MOSS development and customisation with a primary focus on CMS.
    Tuesday, May 18, 2010 5:19 PM
  • It's a open source web part.
    Tuesday, May 18, 2010 5:22 PM
  • OK, i can only assume then that you have access to the code that is rendering your list of content. It is most likely already using the Microsoft API to get the list of content - in which case you just need to locate where it is rendering the link to this "screen where you can open the doc, edit, delete, etc" and change it to point directly to the document itself rather than this "screen".

    As you asked earlier, if you have the ListId and ItemId then you would simply need to use something similar to this to get an actual item URL:

    SPList.GetItemById will return you an SPListItem object using the ItemId - you first need to get an SPList item which you can get from an SPWeb object (the code your webpart uses will most likely already have this available). The SPWeb object Lists collection can give you the SPList object. Once you have the SPListItem object then you can use it's Url property. If you replace the link to the "screen" with this link it will directly open the document or show the save dialog.

    Further API documentation on the SPListItem is available here http://msdn.microsoft.com/en-us/library/ms415089(v=office.12).aspx


    Read my wiki at www.intheknow.it for more code and tips for developing with SharePoint 2007 & 2010
    Twitter: @starznet

    Technical Architect at Starznet Ltd. WSS/MOSS development and customisation with a primary focus on CMS.
    • Marked as answer by Chochobeelai Tuesday, May 18, 2010 5:45 PM
    Tuesday, May 18, 2010 5:29 PM
  • Ok thanks for the help, i'll try your suggestion out.
    Tuesday, May 18, 2010 5:45 PM