Ben 7 May 2007

Hi Folks,

Wondering if anybody can help. I've developing the backend code in c# and using ASP pages for front end (Visual studio as an IDE). I am able to prompt a user for a project name and create a document library in sharepoint with that project name.

My problem is that I can't prevent users from creating projects with duplicate names.

=======================================================

I've tried using the following code but some of it supposedly is already deprecated.

SPSite site = new SPSite(http://x);

SPWeb web = site.OpenWeb();

site.AllowUnsafeUpdates = true;

SPSite impSite = new SPSite(http://x);

string DLName = ProjectNamefld.Text.ToUpper().Trim();

string DSName = RASCIDesc.Text.Trim();

SPListTemplate listTemplate = web.ListTemplates["Document Library"];

SPSearchResultCollection SearchResult = web.SearchListItems(DLName);

int Found = SearchResult.Count;

if (Found == 0)

{

// create the document library

System.Guid guid = web.Lists.Add(DLName, DSName, listTemplate);

}

else

{

// Let the user know that the document library already exists so the document library was not

// created.

}

===============================================================

My understanding is that SearchListItems is deprecated.

So my question is their something similar or better that web.SearchListItems(NameOfProject);

that will let me know that the project already exists and not to go any further.

Code samples on how to do it would be appreciated.

Thank,

Ben



Re: SharePoint - Development and Programming Checking if a Document Library already exists

ganymed

The SearchListItems Method "Searches list data in the Web site for the specified string", but you want to check if a document library already exists. So i would suggest to loop through all existing lists on the web and check their names matching with the given string.



Re: SharePoint - Development and Programming Checking if a Document Library already exists

Ishai Sagi

did you try doing "web.GetList" with the path of the list you are about to create

Code Snippet

bool listExists = true;

try{

SPList list = web.GetList("/Lists/" + DLName) ;

}

catch{listExists =false;}






Re: SharePoint - Development and Programming Checking if a Document Library already exists

Ben 7 May 2007

I would like to say thank you to you both for a quick and useful reponse. Thanks Ganymed for the idea; and thanks to Ishai Sagi. Your code snippet worked very nicely.


Ben Smile