SharePoint - How to programmatically get the "Documents" library for foreign langua

Asked By dsffdz on 28-Jun-10 11:18 AM

Hi,

 

We currently have the following bit of code that gets the "Documents" document library that was created by the Publishing feature to store documents that are used on pages in our site:

 

SPWeb currentWeb = SPContext.Current.Web;

SPDocumentLibrary documentsLib = currentWeb.Lists["Documents"] as SPDocumentLibrary;

 

The trouble is that is works great in English (and French accidentally) but not in Finnish since the Publishing feature creates a "Asiakirjat" document library.

 

Is there a way to get the right library, whatever language is used?

 

TIA

Alon Havivi replied to dsffdz on 28-Jun-10 05:12 PM

Hello,


You can use SharePoint Resource Files (.resx), for the term "Documents" you can use the core.resx files.

I updated your code to pull from the core.resx file the value doucments:


SPWeb currentWeb = SPContext.Current.Web;

uint language = currentWeb != null ? currentWeb.Language : 1033;

string listName = SPUtility.GetLocalizedString("$Resources:core,category_Documents", "core", language);

SPDocumentLibrary documentsLib = currentWeb.Lists[listName] as SPDocumentLibrary;



for Shared Documents you can use this:



SPWeb currentWeb = SPContext.Current.Web;

uint language = currentWeb != null ? currentWeb.Language : 1033;

string listName = SPUtility.GetLocalizedString("$Resources:core,shareddocuments_Title", "core", language);

SPDocumentLibrary documentsLib = currentWeb.Lists[listName] as SPDocumentLibrary;

Devil Scorpio replied to dsffdz on 29-Jun-10 12:33 AM

Hi there,


I do have a document library called "My Documents". I am going to add document to this library. Currently the documents in library are as below.


image1.gif

Working Procedure

1. User will enter full path of the file to be uploaded in the textbox.
2. File will get uploaded with the name uploadaedfromcode.doc in the document library.
3. Window form contains one textbox and one button.
4. Add the reference of Microsoft.SharePoint.dll in the window application.

image1.1.gif

Design

image2.gif

Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using System.IO;

namespace FileUploadinSharePoint
{
 
    public partial class Form1 : Form
    {

      public Form1()
      {
        InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {

      }

      private void btnUpload_Click(object sender, EventArgs e)
      {

        string filename = txtUpload.Text;

        SPSite _MySite = new SPSite("http://adfsaccount:2222/");

        SPWeb _MyWeb = _MySite.OpenWeb();

        FileStream fstream = File.OpenRead(txtUpload.Text);

        byte[] content = new byte[fstream.Length];

        fstream.Read(content, 0, (int)fstream.Length);

        fstream.Close();

        _MyWeb.Files.Add("http://adfsaccount:2222/My%20Documents/uploadedfromcode.doc", content);

        MessageBox.Show("File Saved  to name called uploadedfromcode");

      }
    }
}

Explanation

1. SPSite is returning the site collection. URL of the site collection is being passed to the constructor.
2. SPWeb is returning the top level site.
3. FileStream is reading the file from the path provided in the text box.
4. I am reading the filestream in the byte.
5. I am adding the file in the document library. There are two parameter passed. First parameter is URL of the document library. Second parameter is byte read from the file stream.
6. I am giving name of the file is uploadedfromcode.doc

Output

image3.gif

image4.gif

 

 

Best Regards :)

Timo replied to dsffdz on 18-Jul-11 07:52 AM
In SharePoint 2010, you can use cmscore.resx found in 14/Resources.

using Microsoft.SharePoint.Utilities;
  
string documentsLibraryName = string.empty;
SPWeb currentWeb = SPContext.Current.Web
 
documentsLibraryName = SPUtility.GetLocalizedString("$Resources:cmscore,ListNameDocuments", "cmscore", currentWeb.Language);
 
SPDocumentLibrary documentsLib = currentWeb.Lists[docuemntsLibraryName] as SPDocumentLibrary;