Accessing a SharePoint style library programmatically from C #

Firstly, I am new to C # and SharePoint (less than a month), so apologize if this is an obvious or simple question, but I was wasting the web for a couple of days without much success.

I have an xslt file that I saved in the Styles Library subdirectory from a new website, but how can I access this from within C #?

I have looked at SPSite and SPWeb, but neither can do what I want.

Any help would be greatly appreciated.

Many thanks

C # newbie

+1


source to share


5 answers


Here's some code to extract list items from a list:

SPList list = web.Lists["MyLibrary"];
            if (list != null)
            {
                var results = from SPListItem listItem in list.Items
                              select new 
                              {
                                  xxx = (string)listItem["FieldName"]),
                                  yyy  = (string)listItem["AnotherField"],
                                  zzz = (string)listItem["Field"]
                              };
            }

      



To extract the file, you can also use this method in SPWeb: GetFileAsString

+1


source


Patrick

Hope you enjoy both C # and SharePoint!

Check out the article here .



Read this and he should provide you with all the help you need.

Nick.

0


source


without linq:

int itemId = getItemId();
SPWeb currentWeb = SPContext.Current.Web;
SPList list =  currentWeb.Lists["MyList"];
if ( list != null )
{
     SPListItem theItem = list.Items.GetItemById(itemId);
     doWork(theItem);
}

      

SPWeb can be retrieved in a variety of ways using SPContext, will work if the code is called from SharePoint. To get SPWeb object from url you can use SPSite object ie

using ( SPSite site = new SPSite(urlToWeb) )
{
   using (SPWeb web = site.OpenWeb())
   {
     doWork(web);
   }
}

      

the 'using' statement ensures that non-managed resources are fixed in a timely manner by calling Dispose () on the appropriate objects.

NTN, JT

0


source


Thanks a lot for your help with this. I used a bit of each and did some additional reading and came up with the following:

private static string getXsl()
{
    string xslString = null;
    using (StreamReader streamReader = new StreamReader(
        File.Open(HttpContext.Current.Server.MapPath(@"~_layouts\theXSL.xslt"), FileMode.Open)))
    {
        xslString = streamReader.ReadToEnd();
    }
    return xslString;
}

      

0


source


As efficiently as it can be, you should really learn the best practices as they relate to storing documents in 12 hives and a content database.

There are much more scalable answers to consider before choosing the lemming route.

0


source







All Articles