前段时间刚接触python,发觉它很简单很实用。最近也一直做sharepoint的项目,很自然就想到,能不能使用python来做一些简单的sharepoint? 如果能直接操作sharepoint的对象模型,使用python对sharepoint做一些简单的开发定制应该是可行吧?
    于是花了点时间研究了一下,写一些代码。基本上我是在把对sharepoint对象模型操作封装成.net com对象,然后在python里通过pythonwin的com api操作这些对象。
    下面是代码:
39
 PySP
{
    [ComVisible(true)]
    
public class PyWeb
    {
        
protected SPWeb web = null;

        
protected void EnsureWeb()
        {
            
if (this.web == null)
                
throw new NullReferenceException("the web object can't be null!");
        }

        
public PyWeb() { }

        
public PyWeb(SPSite site, string url)
        {
            
this.web = site.OpenWeb(url);
        }

       
        
public bool AllowAnonymousAccess
        {
            
get
            {
                EnsureWeb();
                
return this.web.AllowAnonymousAccess;
            }

        }

        

        
public bool AllowRssFeeds
        {
            
get
            {
                EnsureWeb();
                
return this.web.AllowRssFeeds;
            }

        }

        
public bool AllowUnsafeUpdates
        {
            
get
            {
                EnsureWeb();
                
return this.web.AllowUnsafeUpdates;
            }
            
set
            {
                EnsureWeb();
                
this.web.AllowUnsafeUpdates = value; 
            }
        }


        
public string Title
        {
            
get
            {
                EnsureWeb();
                
return this.web.Title;
            }
            
set
            {
                EnsureWeb();
                
this.web.Title = value;
            }
        }

        
public string Description
        {
            
get
            {
                EnsureWeb();
                
return this.web.Description;
            }
            
set
            {
                EnsureWeb();
                
this.web.Description = value;
            }
        }

        
public string Name
        {
            
get
            {
                EnsureWeb();
                
return this.web.Name;  
            }
            
set
            {
                EnsureWeb();
                
this.web.Name = value;
            }
        }
         

        
public uint Language
        {
            
get
            {
                EnsureWeb();
                
return this.web.Language;  
            }
        }

        
public void Update()
        {
            EnsureWeb();
            
this.web.Update(); 
        }

        
public void Delete()
        {
            EnsureWeb();
            
this.web.Delete();  
        }

        
public void Close()
        {
            
if (this.web != null)
            {
                
this.web.Dispose();
                
this.web = null;
            }
        }

        
public PyListCollection Lists
        {
            
get
            {
                EnsureWeb();
                
return new PyListCollection(this.web.Lists);
            }
        }

    }
}
 PySP
{
    [ComVisible(true)]
    
public class PyList
    {
        
protected SPList list = null;

        
protected void EnsureList()
        {
            
if (this.list == null)
                
throw new NullReferenceException("the list object can't be null!");
        }

        
public PyList() { }

        
public PyList(SPList list)
        {
            
this.list = list;
        }

        
public string Title
        {
            
get
            {
                EnsureList();
                
return this.list.Title;
            }
            
set
            {
                EnsureList();
                
this.list.Title = value;
            }
        }

        
public bool AllowContentTypes
        {
            
get
            {
                EnsureList();
                
return this.list.AllowContentTypes;
            }
        }

        
public bool AllowDeletion
        {
            
get
            {
                EnsureList();
                
return this.list.AllowDeletion;
            }
        }

        
public bool CanReceiveEmail
        {
            
get
            {
                EnsureList();
                
return this.list.CanReceiveEmail;
            }
        }

        
public DateTime Created
        {
            
get
            {
                EnsureList();
                
return this.list.Created;
            }
        }

        
public PyListItemCollection GetAllItems()
        {
            EnsureList();
            
return new PyListItemCollection(this.list.Items);
        }



        
public void Update()
        {
            EnsureList();
            
this.list.Update();
        }
    }
}
 PySP
{
    [ComVisible(true)]
    
public  class PyListCollection
    {
        
protected SPListCollection lists;

        
protected void EnsureLists()
        {
            
if (this.lists == null)
                
throw new NullReferenceException("the lists object can't be null!");
        }

        
public PyListCollection() { }

        
public PyListCollection(SPListCollection lists)
        {
            
this.lists = lists;
        }

        
public int Count
        {
            
get
            {
                EnsureLists();
                
return this.lists.Count;
            }
        }

        
public PyList GetListByIndex(int index)
        {
            EnsureLists();
            
return  new PyList(this.lists[index]);
        }

        
public PyList GetListByName(string name)
        {
            EnsureLists();
            
return new PyList(this.lists[name]);
        }

    }
}
 PySP
{
    [ComVisible(true)]
    
public class PyListItem
    {
        
protected SPListItem item = null;

        
protected void EnsureItem()
        {
            
if (item == null)
                
throw new NullReferenceException("the item object can't be null !");
        }

        
public PyListItem() { }

        
public PyListItem(SPListItem item)
        {
            
this.item = item;  
        }

        
public string Title
        {
            
get
            {
                EnsureItem();
                
return this.item.Title;
            }
            
        }

        
public void Update()
        {
            EnsureItem();
            
this.item.Update();
        }
    }
}
44

编译后生成pyps.dll,然后注册com组件,运行命令regasm pyps.dll / register

下面是python 代码:

 

        

 

最后是python的简单测试代码:

>>site=PySite('http://sun/')

>>web=site.OpenWeb('/test')

>>print  web.Title

>>'Moss'

>>web.Title='MyMoss'

>>web.Update()

>>print web.Title

>>'MyMoss'

这是一时无聊写下的代码,本身也没有什么实用价值。

不过还是希望大虾们指点一下,这样写会有什么副作用没有?

谢谢.

相关文章: