Share Point Sample Code

  • View
    4

  • Download
    0

Embed Size (px)

Text of Share Point Sample Code

SharePoint Tips & Code 1.Here is a bit of code to retrieve the 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 retrieve a file you could also use this method on SPWeb: GetFileAsString 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); } The SPWeb can be retrieved in numerous ways, using the SPContext will work if the code is called from SharePoint. To get an SPWeb object from a URL you can use SPSite object i.e. using ( SPSite site = new SPSite(urlToWeb) ) { using (SPWeb web = site.OpenWeb()) { doWork(web); } } the 'using' statement ensures non-managed resources are reclaimed in a timely manner, by calling 'Dispose()' on the relevant objects. Effective as that may be, you should really look into best practices as they relate to storing documents in the 12 hive versus the content database. 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(); } Page | 1

SharePoint Tips & Code return xslString; }

2.Using linq with Sharepoint and disposing of objects

The technical answer to your original question is a qualified "No": all SPWeb objects opened from an SPSite are automatically disposed when the SPSite is disposed. However, in practice it is a good idea to dispose an SPWeb as soon as you're done with it to reduce memory pressure, especially when working with code like this that opens several SPWeb objects. Implementing this dispose-safe behavior for LINQ is actually quite simple in C#. You can find full details in this post, but the short version is that a C# iterator can handle disposal for you. Using my AsSafeEnumerable() extension method, your code is relatively safe written like this: using (SPSite spSite = Utility.GetElevatedSite(_rootUrl)) { var sw = from SPWeb web in spSite.AllWebs.AsSafeEnumerable() where web.ServerRelativeUrl.ToLower() == path from SPWeb subWeb in web.Webs.AsSafeEnumerable() select subWeb; foreach(SPWeb aSubWeb in sw) { // Do something } } Now the result of your query, which I've assigned to sw, is a lazy iterator of type IEnumerable. As you enumerate over that result, each SPWeb will be disposed when the enumerator moves to the next item. This means that it is not safe to use that SPWeb reference, or any SP* object created from it (SPList, etc), outside of your foreach loop. Also sw would not be safe to use outside of that using block because the iterator's SPWebCollections will be tied to the now-disposed SPSite. That said, code like this that enumerates over all webs (twice!) is extremely expensive. There is almost certainly a more efficient way that this could be implemented, if only by using spSite.AllWebs[path] instead of your from/where. Regarding garbage collection, these objects require disposal because of unmanaged memory allocated that the GC doesn't even know about. Finally, a word of caution about your 'GetElevatedSite' utility. If you're using RunWithElevatedPrivileges in your helper method to get your elevated SPSite, there are a number of issues you could run into by returning your SPSite out of that elevated context. If possible, I would suggest using SPSite impersonation instead - my preferred method is described here. Q: When should you dispose SPWeb and SPSite objects? And even more important, when not?

Page | 2

SharePoint Tips & Code A: You should always dispose them if you created them yourself, but not otherwise. You should never dispose SPContext.Current.Web/Site and you should normally not dispose SPWeb if IsRootWeb is true. More tricky constructs are things along the line of SPList.ParentWeb.

3.Sharepoint API - How to Upload files to Sharepoint Doc Library from ASP.NET Web Application You can write some custom code to do it. You could use the SharePoint API if you are on the same server or use WebServices Here is the sample code assuming that you know the url of the document library and you are uploading the document to the root folder. You will have to add Microsoft.SharePoint.dll as reference to your ASP.NET project using (SPSite siteCollection = new SPSite(url)) { using (SPWeb spWeb = siteCollection.OpenWeb()) { SPList spList = spWeb.GetList(url); string fileName = "XXXX"; FileStream fileStream = null; Byte[] fileContent = null; try { string docPath = XXXX; //physical location of the file fileStream = File.OpenRead(docPath + fileName); fileContent = new byte[Convert.ToInt32(fileStream.Length)]; fileStream.Read(fileContent, 0, Convert.ToInt32(fileStream.Length)); spList.RootFolder.Files.Add(spList.RootFolder.Url + "/" + fileName, fileContent, true); spList.Update(); } catch(Exception ex) { } finally { if (fileStream != null) { fileStream.Close(); } } } } Page | 3

SharePoint Tips & Code

4.SPSite, SPWeb and SPWebCollection http://www.sharepointblogs.com/mingssn/archive/2007/10/10/sharepoint-programming.aspx Sharepoint come with many class. Some class name is confusing. SPSite: Represents a collection of sites on a virtual server, including a top-level site and all its subsites. Each SPSite object, or site collection, is represented within an SPSiteCollection object that consists of the collection of all site collections on the virtual server. Note: SPSite is NOT a Collection, in another words, you can not use foreach to loop SPSite. To do this, you have to use SPWebCollection. SPWebCollection: Represents a collection of SPWeb objects. SPWeb: Represents a SharePoint Web site. SPFolder: Represents a folder on a SharePoint Web site. The following snipet is copy from a class: #region properties /// /// site collection url address /// public string _RootSiteUrl { get { return RootSiteUrl; } set { RootSiteUrl = value; } } /// /// Site collection, get SPSite object repsent a "collection" /// public SPSite _Sites { get { return new SPSite(this._RootSiteUrl); } set { Sites = value; } } /// /// Web collection, SPWebCollection, this is the real Collection /// public SPWebCollection _Webs { Page | 4

SharePoint Tips & Code get { return this._Sites.AllWebs; } } public string _PPMAspxPageSiteUrl { get { return this.PPMTargetSiteUrl; } set { PPMTargetSiteUrl = value; } } public SPWeb this[string SiteName] // indexer, represent a SPWeb { get { int i = 0; foreach (SPWeb web in this._Webs) { if (web.Url == SiteName) return this._Webs[ i ]; i++; } return null; } } #endregion The indexer is quite useful, to get a SPWeb object, simply use mySharePointClass["your site or subsite url"] to access!

Sharepoint tips 5.Enable Sharepoint server side script

By default, Sharepoint does not allow server side script for security reason. To get around this, you can modify your web.config to specify which page(s) you allow server side script. For example, you can add following code to your Sharepoint application web.config. ( I am using /en-ca/pages/* as example ) I found this is very useful during development if you are using in-line script for your aspx pages. Lets say you have a SP project including some customrized aspx pages. Youve created feature and a wsp file to install in your Sharepoint. Now you noticed you have to do some change to the in-line script of the aspx file. Of course, you could re-deploy the entire wsp again, but as a developer, you dont want do this because most likely you have to modify the aspx again and again Following is the steps I am using Page | 5

SharePoint Tips & Code 1. Load aspx pages from Sharepoint UI and publish it 2. Reset IIS 3. Load this aspx page from your browser Sharepoint will give you error message saying server script is not allowed. OK, thats fine, modify web.config add and reload the page. Yeah, everything is working and you can see your changes. Someting you should also know 1. It is better using code behind for the aspx pages in your SP project. By using code behind, when you change any logic of your aspx pages, you only rep-deploy dlls, not the page itself. You dont need modify web.config. 2. Why this does not happen before you change and load the aspx page to the SP? That page also contains script, but SP did not give any error at that time. I will explain this in my next post Sharepoint ghost

6.Backup/restore sharepoint content database

Ive read some articles of how to backup/restore Sharepoint application. Of course you can use stsadm -o backup&restore to do the job but you can also backup/restore content database from sql server directly. Even this is not recommended by some Sharepoint guru but I believeit is good to know and it might work in your scenario (for example, move production/staging/QA envirnment to your dev envirnment). Ok, here are the steps. 1. Backup your sharepoint content database from sql server 2. Go to Sharepoint Centre Admin->Application Management->Content databases. Locate your sharepoint application and remove the content database. This will basically break the link between your sharepoint application and your content database. If you dont do this, the link always exists and you will not be able to restore the content database later because sql server thinks the content database is still in use, and you will find you cant kill that process in the sql server. 3. Delete and recreate you local sharepoint content database ( what? r u crazy? ) 4. Restore the content database you did in step 1 5. Go to same place as step 2 and attach the content database. Note, you might get a error message saying the sql database c