Thursday, June 19, 2008

Get a SharePoint list (SPList) Root Folder Name and Display Name (Title)

style="FONT-SIZE: 16px; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #333; PADDING-TOP: 10px">Summary


style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 5px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">href="#Introduction">1. Introduction
2. Samples

1.1. Getting List names
programmatically in c#

href="#Retrieving-a-list-by-its-names-programmatically-in-C#">1.2. Retrieving a
list by its names programmatically in C#

href="#document-Library">1 - Document Library
href="#Standard-Lists-">2 - Standard Lists
3.
Warnings

1. Special characters
href="#File-Not-Found-exception">2. File Not Found exception


style="FONT-SIZE: 16px; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #333; PADDING-TOP: 10px">name=Introduction>1.Introduction


style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 5px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">Reading
MSDN for href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.aspx">SPList
Class
, it seems it's not very obvious to:
get the name of a SharePoint
list.
look for a list in a SharePoint web site using its name.
Here is
the only excerpt of the href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.aspx">SPList
Class Library Reference
in MSDN speaking of the list name:
 

[...]
Use an indexer to return a single list from the collection. For
example, if the collection is assigned to a variable named collLists, use
collLists[index] in C#, or collLists(index) in Visual Basic 2005, where index is
the index number of the list in the collection, the display name of the list, or
the GUID of the list.
[...]


style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 5px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">For
instance SPList class has no Name property or GetName method.
Furthermore, a
SharePoint list will have TWO names if you had renamed it!
The Root Folder
Name (like the Internal Name for a SPList Field ) is the one you have set when
you have created the list.
This name will be always present in your browser
Address Bar when you display the list
The display name (the one you have
used if you have renamed the list).
This name will appear as your list Title,
when you display the list and the as a link to the list in your QuickLaunch menu
if you have added to. (If you have not renamed your List, the root folder name
and the display name are the same).


style="PADDING-RIGHT: 0px; PADDING-LEFT: 13px; FONT-WEIGHT: bold; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #e9ab17; PADDING-TOP: 10px">name=Samples>2.Samples


style="PADDING-LEFT: 26px; FONT-WEIGHT: normal; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #e9ab17; PADDING-TOP: 10px">name=Getting-List-names-programmatically-in-c#>1.1 Getting List names
programmatically in c#


style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">So,
how to get a SharePoint list Root Folder Name and Display Name (Title) in C#
programming.
The following code sample will display these two names for all
lists of a SharePoint web site:


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">color=#000000>                           
SPWeb
myWeb =
SPContextcolor=#000000>.Current.Web;


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">color=#000000>style="mso-spacerun: yes">                           
Debug.WriteLine(
"MyWeb lists :
"
);


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">color=#000000>                           
foreach
(
SPList aList
in
myWeb.Lists)


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">color=#000000>style="mso-spacerun: yes">                           
{


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">color=#000000>style="mso-spacerun: yes">                               
Debug.WriteLine(
style="COLOR: #a31515">"************************************"color=#000000>);


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">color=#000000>style="mso-spacerun: yes">                               
Debug.WriteLine(
"list Title (Display
Name): "
+ aList);


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">color=#000000>style="mso-spacerun: yes">                               
Debug.WriteLine(
"list Title (Display
Name): "
+ aList.Title);


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">color=#000000>style="mso-spacerun: yes">                               
Debug.WriteLine(
"list Root Folder
Name: "
+
aList.RootFolder.Name);


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">color=#000000>                               
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">color=#000000>Debug.WriteLine(style="COLOR: #a31515">"************************************"color=#000000>);


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">color=#000000>style="mso-spacerun: yes">                           
}


style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">

style="PADDING-LEFT: 26px; FONT-WEIGHT: normal; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #e9ab17; PADDING-TOP: 10px">name=Retrieving-a-list-by-its-names-programmatically-in-C#>2.2 Retrieving a
list by its names programmatically in C#


style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">And
now, how to retrieve a SharePoint list using Root Folder Name or Display Name:

Notice that:
Document Libraries URL finishes by
"/Website/DocLibRootFolderName"
Other Lists URL finishes by
"/Website/Lists/OtherListsRootFolderName"


style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">1 -
Document Library


style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">Assume
we need to instantiate a SPList object corresponding to a Document Library with,

Root Folder Name: mycustomdoclib1
Display Name: Invoices
the
following code sample will illustrate that:



    style="padding-LEFT: 50px;padding-LEFT: 50px;MARGIN-TOP: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; MARGIN-BOTTOM: 5px; COLOR: gray; FONT-FAMILY: arial">
  • style="MARGIN-TOP: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; MARGIN-BOTTOM: 5px; COLOR: gray; FONT-FAMILY: arial; TEXT-ALIGN: justify">Root
    Folder Name:

style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                     
style="mso-spacerun: yes">      style="COLOR: #2b91af">SPWeb myWeb = style="COLOR: #2b91af">SPContext.Current.Web;


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
//SPList myList =
myWeb.GetList("sites/my-collaboration-portal/docs/mycustomdoclib1");


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
SPList myList = myWeb.GetList(style="COLOR: #2b91af">SPUrlUtility.CombineUrl(myWeb.ServerRelativeUrl,
"mycustomdoclib1"));


    style="padding-LEFT: 50px;MARGIN-TOP: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; MARGIN-BOTTOM: 5px; COLOR: gray; FONT-FAMILY: arial">
  • style="MARGIN-TOP: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; MARGIN-BOTTOM: 5px; COLOR: gray; FONT-FAMILY: arial; TEXT-ALIGN: justify">Display
    Name:

style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
SPList myList = myWeb.Lists[style="COLOR: #a31515">"Invoices"];


style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">2 -
Standard Lists


style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">Assume 
we need to instantiate a SPList object corresponding to a list that is not a
document library with,
Root Folder Name: mycustomlist1
Display Name:
Clients
the following code sample will illustrate that:


    style="padding-LEFT: 50px;MARGIN-TOP: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; MARGIN-BOTTOM: 5px; COLOR: gray; FONT-FAMILY: arial">
  • style="MARGIN-TOP: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; MARGIN-BOTTOM: 5px; COLOR: gray; FONT-FAMILY: arial; TEXT-ALIGN: justify">Root
    Folder Name:

style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
SPWeb myWeb = style="COLOR: #2b91af">SPContext.Current.Web;


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
//SPList myList =
myWeb.GetList("sites/my-collaboration-portal/docs/Lists/ mycustomlist1 ");


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
SPList myList = myWeb.GetList(style="COLOR: #2b91af">SPUrlUtility.CombineUrl(myWeb.ServerRelativeUrl,
"/Lists/ mycustomlist1
"
)); 

 


    style="padding-LEFT: 50px;MARGIN-TOP: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; MARGIN-BOTTOM: 5px; COLOR: gray; FONT-FAMILY: arial">
  • style="MARGIN-TOP: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; MARGIN-BOTTOM: 5px; COLOR: gray; FONT-FAMILY: arial; TEXT-ALIGN: justify">Display
    Name:

style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
SPList myList = myWeb.Lists[style="COLOR: #a31515">"Clients"];


style="FONT-SIZE: 16px; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #333; PADDING-TOP: 10px">3.
Warnings:


style="PADDING-RIGHT: 0px; PADDING-LEFT: 13px; FONT-WEIGHT: bold; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #e9ab17; PADDING-TOP: 10px">3.1
Special characters


style="PADDING-RIGHT: 5px; PADDING-LEFT: 18px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">When
you set your list display name, you will generally do it using the Web Site user
language. If this language is not English, you will sometimes have to use
special characters like:
É, è , ù, ê, Ø, ñ, ô, …

SharePoint generally
use Unicode escape sequence for these characters. Assume your list display name
in French is:
"ma première doclib",

you will have to use that string
to retrieve it:


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">style="mso-spacerun: yes">color=#000000>                           
SPList
myList = myWeb.Lists[
"ma premi\u00e8re
doclib"
];


style="PADDING-RIGHT: 5px; PADDING-LEFT: 18px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">
If
you are looking for any Unicode escape sequence, you can download a mapping
table here.


style="PADDING-RIGHT: 5px; PADDING-LEFT: 18px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">If
you have too many fields with special characters to manipulate, think to use
Visual Studio Advanced Save Options and save your .aspx Page file in "UTF-8 with
signature" or "Unicode" format, doing that will allow you to write your fields
name using your language special characters. You can also modify your Web
Application web.config to obtain the same result.


style="PADDING-RIGHT: 5px; PADDING-LEFT: 18px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">About
this topic you can read this post:

href="http://www.aspnetresources.com/blog/unicode_in_vsnet.aspx">Unicode in
Visual Studio .Net


style="PADDING-LEFT: 26px; FONT-WEIGHT: normal; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #e9ab17; PADDING-TOP: 10px">Tricks:


style="PADDING-RIGHT: 5px; PADDING-LEFT: 18px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">To avoid using special characters in your requests:
Use an English name
for the list when you create it, and rename your list using Web Site user
language.
Use the list Internal Name with SPWeb.GetList method to retrieve a
list using server code (VB .net, C#...).


style="PADDING-RIGHT: 0px; PADDING-LEFT: 13px; FONT-WEIGHT: bold; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #e9ab17; PADDING-TOP: 10px">3.2
File Not Found exception


style="PADDING-RIGHT: 5px; PADDING-LEFT: 18px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">There
is a lack in SharePoint when you are trying to find objects, because the methods
you are using for that will throw an exception if the object does not exist
instead of returning null. That forces you to manage certain parts of your code
by handling exception that is not a good coding practice.


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
SPList
listVariable;


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
try


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
{


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                               
listVariable = myWeb.Lists[style="COLOR: #a31515">"ListIWant"];


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">style="mso-spacerun: yes">                           
catch (style="COLOR: #2b91af">Exception e) { }



style="PADDING-RIGHT: 5px; PADDING-LEFT: 18px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">There
is at least two ways of avoiding exception handling when trying to retrieve a
list:


style="PADDING-LEFT: 26px; FONT-WEIGHT: normal; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #e9ab17; PADDING-TOP: 10px">1
- Loop-through-the-lists


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
SPList
listVariable;


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
foreach (style="COLOR: #2b91af">SPList tempList style="COLOR: blue">in myWeb.Lists)


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                           
{


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                               
if (tempList.Title == style="COLOR: #a31515">"ListIWant")


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                               
{


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                                   
listVariable = tempList;


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">style="mso-spacerun: yes">                                   
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">breakstyle="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">;


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">style="mso-spacerun: yes">                               
}


style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left>style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">style="mso-spacerun: yes">                           
}
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes">




style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">It
is not so fool as it seems to be since someone found that loop was always faster
than the previously described methods.


style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">href="http://www.novolocus.com/2009/01/29/fast-access-items-in-an-splistcollection/">Fast
access items in an SPListCollection


style="PADDING-LEFT: 26px; FONT-WEIGHT: normal; PADDING-BOTTOM: 10px; MARGIN: 1px 0px; COLOR: #e9ab17; PADDING-TOP: 10px">2
- Use-LINQ


style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">There
is a LINQ request posted by Adam Buenz that allows you to look for a list
without throwing an exception if it does not exists.

By the way the Linq request actually is doing a loop.



style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none">style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-fareast-language: EN-US">style="mso-spacerun: yes">       
public style="COLOR: blue">static bool
InspectForList(string
listName)


style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none">style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-fareast-language: EN-US">style="mso-spacerun: yes">       
{


style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none">style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-fareast-language: EN-US">style="mso-spacerun: yes">           
var results = style="COLOR: #2b91af">SPContext.Current.Web.Lists.Cast<style="COLOR: #2b91af">SPList>().Where(item => Equals(style="COLOR: blue">string.Compare(item.Title, listName, style="COLOR: blue">true), 0));


style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none">style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN-US; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-fareast-language: EN-US">style="mso-spacerun: yes">           
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-fareast-language: EN-US">returnstyle="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-fareast-language: EN-US">
results.Count() > 0;


style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none">style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-fareast-language: EN-US">style="mso-spacerun: yes">        }




style="PADDING-RIGHT: 5px; PADDING-LEFT: 31px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">href="http://www.sharepointsecurity.com/blog/general/what-are-the-biggest-sharepoint-api-mistakes/">What
Are The Biggest SharePoint API Mistakes?


style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 5px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">

style="PADDING-RIGHT: 5px; PADDING-LEFT: 18px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify">For
ending, a last advice,
You should always use the root folder name of a list
to look for it, since this name will never change.


style="PADDING-RIGHT: 5px; PADDING-LEFT: 18px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify"> 


style="PADDING-RIGHT: 5px; PADDING-LEFT: 18px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; PADDING-BOTTOM: 18px; MARGIN: 1px 0px; COLOR: gray; PADDING-TOP: 5px; FONT-FAMILY: arial; TEXT-ALIGN: justify"> 


 


No comments:

Post a Comment