sharepoint 2010 中操作List

  Sharepoint 内置了几种列表类型:

  


public enum SPBaseType
{
    UnspecifiedBaseType 
= -1,
    GenericList 
= 0,
    DocumentLibrary 
= 1,
    Unused 
= 2,
    DiscussionBoard 
= 3,
    Survey 
= 4,
    Issue 
= 5,
}

 

 

新建列表
 /// <summary>
        
/// 新建列表
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void btn_Click(object sender, EventArgs e)
        {
            
using (SPSite site = new SPSite(requestUrl))
            {
                
using (SPWeb web = site.OpenWeb())
                {
                    SPList list 
= null;
                    
string listName = txtListName.Text.Trim();
                    
// Check whether the list already exists
                    try
                    {
                        list 
= web.Lists[listName];
                    }
                    
catch (ArgumentException)
                    {
                    }
                    
if (list == null)
                    {
                        Guid listId 
= web.Lists.Add(listName, "All our books",SPListTemplateType.GenericList);
                        list 
= web.Lists[listId];
                        list.OnQuickLaunch 
= true;
                        list.Update();
                    }
                }
            }
        }

 

 

判断列表是否存在
        
        
/// <summary>
        
/// 判断列表是否存在
        
/// </summary>
        
/// <param name="listName"></param>
        
/// <returns></returns>
        private bool IsListExist(string listName)
        {
            
using (SPSite site = new SPSite(requestUrl))
            {
                
using (SPWeb web = site.OpenWeb())
                {
                    
bool isExists = (from l in web.Lists.OfType<SPList>()
                                     
where l.Title.Equals(listName)
                                     select l).Count() 
> 0;
                    
return isExists;
                }
            }
        }

 

 


 创建栏


        
/// <summary>
        
/// 创建栏
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void btnAddField_Click(object sender, EventArgs e)
        {
            
using (SPSite site = new SPSite(requestUrl))
            {
                
using (SPWeb web = site.OpenWeb())
                {
                    
string listName = txtFieldList.Text.Trim();
                    SPList list 
= web.Lists[listName];
                    list.Fields.Add(
"ISBN", SPFieldType.Text, true);
                    list.Fields.Add(
"LeadAuthor", SPFieldType.Text, true);
                    list.Fields.Add(
"Price", SPFieldType.Currency, false);

                    list.Update();
                }
            }
        }

 

 

 

删除栏
        /// <summary>
        
/// 删除栏
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void btnAddField_Click(object sender, EventArgs e)
        {
            
using (SPSite site = new SPSite(requestUrl))
            {
                
using (SPWeb web = site.OpenWeb())
                {
                    
string listName = txtFieldList.Text.Trim();
                    SPList list 
= web.Lists[listName];
                    
//list.Fields.Delete("Price");     //删除栏

                    
//list.Update();
                }
            }
        }

 

 

 

 修改栏


        
/// <summary>
        
/// 修改栏
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void btnAddField_Click(object sender, EventArgs e)
        {
            
using (SPSite site = new SPSite(requestUrl))
            {
                
using (SPWeb web = site.OpenWeb())
                {
                    
string listName = txtFieldList.Text.Trim();
                    SPList list 
= web.Lists[listName];
                    
                    
//修改栏
                    
//list.Fields["标题"].Title = "Title";     
                    
//list.Fields["标题"].Update();

                    
//list.Update();
                }
            }
        }

 

 

 添加项


            
using (SPSite site = new SPSite(requestUrl))
            {
                
using (SPWeb web = site.OpenWeb())
                {
                    SPList bookList 
= web.Lists["Books"];
                    SPListItem item 
= bookList.Items.Add();
                    item[
"Title"= "流氓是怎么炼成的";
                    item[
"ISBN"= "12345";
                    item[
"LeadAuthor"= "abc";
                    item.Update();
                }
            }

 

 

 

 

 

posted @ 2010-11-06 12:14  i'm zjz  阅读(1075)  评论(1编辑  收藏  举报