sharepoint:python操作sharepoint对象模型

  前段时间刚接触python发觉它很简单很实用最近也直做sharepo项目很自然就想到能不能使用python来做些简单sharepo? 如果能直接操作sharepo对象模型使用python对sharepo些简单开发定制应该是可行吧?

   于是花了点时间研究了些代码基本上我是在把对sharepo对象模型操作封装成.net com对象,然后在python里通过pythonwincom api操作这些对象

   下面是代码:

  PySite

 1 using ;
 2 using .Collections.Generic;
 3 using .Text;
 4 using Microsoft.SharePo;
 5 using .Runtime.InteropServices;
 6
 7 PySP
 8 {
 9     [ComVisible(true)]
10     public PySite
11     {
12         protected SPSite site = null;
13
14         public PySite { }
15
16         public void Open( url) { this.site = SPSite(url); }
17
18         public void Close
19         {
20             (this.site != null)
21             {
22                 site.Dispose;
23                 site = null;
24             }
25         }
26
27         public PyWeb OpenWeb( url)
28         {
29             (this.site != null)
30             {
31                 PyWeb(this.site, url);
32             }
33
34             null;
35         }
36        
37     }
38 }
39


  PyWeb

using ;
using .Collections.Generic;
using .Text;
using Microsoft.SharePo;
using .Runtime.InteropServices;

PySP
{
    [ComVisible(true)]
    public PyWeb
    {
        protected SPWeb web = null;

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

        public PyWeb { }

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

      
        public bool AllowAnonymousAccess
        {
            get
            {
                EnsureWeb;
                this.web.AllowAnonymousAccess;
            }

        }

       

        public bool AllowRssFeeds
        {
            get
            {
                EnsureWeb;
                this.web.AllowRssFeeds;
            }

        }

        public bool AllowUnsafeUpdates
        {
            get
            {
                EnsureWeb;
                this.web.AllowUnsafeUpdates;
            }
           
            {
                EnsureWeb;
                this.web.AllowUnsafeUpdates = value;
            }
        }


        public Title
        {
            get
            {
                EnsureWeb;
                this.web.Title;
            }
           
            {
                EnsureWeb;
                this.web.Title = value;
            }
        }

        public Description
        {
            get
            {
                EnsureWeb;
                this.web.Description;
            }
           
            {
                EnsureWeb;
                this.web.Description = value;
            }
        }

        public Name
        {
            get
            {
                EnsureWeb;
                this.web.Name; 
            }
           
            {
                EnsureWeb;
                this.web.Name = value;
            }
        }
        

        public u Language
        {
            get
            {
                EnsureWeb;
                this.web.Language; 
            }
        }

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

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

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

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

    }
}


  PyList

using ;
using .Collections.Generic;
using .Text;
using Microsoft.SharePo;
using .Runtime.InteropServices;

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

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

        public PyList { }

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

        public Title
        {
            get
            {
                EnsureList;
                this.list.Title;
            }
           
            {
                EnsureList;
                this.list.Title = value;
            }
        }

        public bool AllowContentTypes
        {
            get
            {
                EnsureList;
                this.list.AllowContentTypes;
            }
        }

        public bool AllowDeletion
        {
            get
            {
                EnsureList;
                this.list.AllowDeletion;
            }
        }

        public bool CanReceiveEmail
        {
            get
            {
                EnsureList;
                this.list.CanReceiveEmail;
            }
        }

        public DateTime Created
        {
            get
            {
                EnsureList;
                this.list.Created;
            }
        }

        public PyListItemCollection GetAllItems
        {
            EnsureList;
            PyListItemCollection(this.list.Items);
        }
        public void Update
        {
            EnsureList;
            this.list.Update;
        }
    }
}


  PyListCollection

using ;
using .Collections.Generic;
using .Text;
using Microsoft.SharePo;
using .Runtime.InteropServices;

PySP
{
    [ComVisible(true)]
    public  PyListCollection
    {
        protected SPListCollection lists;

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

        public PyListCollection { }

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

        public Count
        {
            get
            {
                EnsureLists;
                this.lists.Count;
            }
        }

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

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

    }
}


  PyListItem

using ;
using .Collections.Generic;
using .Text;
using Microsoft.SharePo;
using .Runtime.InteropServices;

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

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

        public PyListItem { }

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

        public Title
        {
            get
            {
                EnsureItem;
                this.item.Title;
            }
           
        }

        public void Update
        {
            EnsureItem;
            this.item.Update;
        }
    }
}


  PyListItemCollection

 1 using ;
 2 using .Collections.Generic;
 3 using .Text;
 4 using Microsoft.SharePo;
 5 using .Runtime.InteropServices;
 6
 7 PySP
 8 {
 9     [ComVisible(true)]
10     public PyListItemCollection
11     {
12         protected SPListItemCollection items = null;
13
14         protected void EnsureListItems
15         {
16             (items null)
17                 throw NullReferenceException("the items object can't be null!");
18         }
19
20         public PyListItemCollection { }
21
22         public PyListItemCollection(SPListItemCollection items)
23         {
24             this.items = items;
25             EnsureListItems;
26         }
27
28         public Count
29         {
30             get
31             {
32                 EnsureListItems;
33                 this.items.Count;
34             }
35         }
36
37         public PyListItem GetItemByIndex( index)
38         {
39             EnsureListItems;
40             PyListItem(this.items[index]);
41         }
42     }
43 }
44


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

  下面是python 代码:

  pspy.py

  1import win32com.client
  2 PySite:
  3    def __init__(self,url):
  4        self.__com__=win32com.client.Dispatch("PySP.PySite")
  5        self.__com__.open(url)
  6    def OpenWeb(self,url):
  7        pyweb(self.__com__.openweb(url))
  8    def Close(self):
  9        self.__com__.close
 10 
 11
 12 PyWeb(object):
 13    def __init__(self,com):
 14        self.__com__=com
 15    @property
 16    def AllowAnonymousAccess(self):
 17        self.__com__.AllowAnonymousAccess
 18    @property
 19    def AllowAutomaticASPXPageIndexing(self):
 20        self.__com__.AllowAnonymousAccess
 21    @AllowAutomaticASPXPageIndexing.ter
 22    def AllowAutomaticASPXPageIndexing(self,bool):
 23        self.__com__.AllowAutomaticASPXPageIndexing=bool
 24    @property
 25    def AllowRssFeeds(self):
 26        self.__com__.AllowAnonymousAccess
 27    @property
 28    def AllowUnsafeUpdates(self):
 29        self.__com__.AllowAnonymousAccess
 30    @AllowUnsafeUpdates.ter
 31    def AllowUnsafeUpdates(self,value):
 32        self.__com__.AllowUnsafeUpdates=value
 33    @property
 34    def Title(self):
 35        self.__com__.Title
 36    @Title.ter
 37    def Title(self,value):
 38        self.__com__.Title=value
 39    def Update(self):
 40        self.__com__.Update
 41    def Close(self):
 42        self.__com__.Close
 43    @property
 44    def Lists(self):
 45        pylistcollection(self.__com__.Lists)
 46
 47 PyList(object):
 48    def __init__(self,com):
 49        self.__com__=com
 50    @property
 51    def Title(self):
 52        self.__com__.Title
 53    @Title.ter
 54    def Title(self,value):
 55        self.__com__.Title=value
 56    @property
 57    def AllowContentTypes(self):
 58        self.__com__.AllowContentTypes
 59    @property
 60    def AllowDeletion(self):
 61        self.__com__.AllowDeletion
 62    @property
 63    def CanReceiveEmail(self):
 64        self.__com__.CanReceiveEmail
 65    @property
 66    def Created(self):
 67        self.__com__.Created
 68    @property
 69    def Items(self):
 70        pylistitemcollection(self.__com__.GetAllItems)
 71    def update(self):
 72        self.__com__.Update
 73
 74 PyListCollection(object):
 75    def __init__(self,com):
 76        self.__com__=com
 77    @property
 78    def Count(self):
 79        self.__com__.Count
 80    def getList(self,index=-1,name=''):
 81        index!=-1:
 82            pylist(self.__com__.GetListByIndex(index))
 83        name!='':
 84            pylist(self.__com__.GetListByName(name))
 85
 86 PyListItem(object):
 87    def __init__(self,com):
 88        self.__com__=com
 89    @property
 90    def Title(self):
 91        self.__com__.Title
 92    def Update(self):
 93        self.__com__.Update
 94
 95 PyListItemCollection(object):
 96    def __init__(self,com):
 97        self.__com__=com
 98    @property
 99    def Count(self):
100        self.__com__.Count
101    def getItem(self,index=-1):
102          pylistitem(self.__com__.GetItemByIndex(index))
103   
104
105   






  最后是python简单测试代码:

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

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

>>pr  web.Title

>>'Moss'

>>web.Title='MyMoss'

>>web.Update

>>pr web.Title

>>'MyMoss'


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

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

  谢谢.



Tags:  sharepoint论坛 sharepoint教程 sharepoint2007 sharepoint

延伸阅读

最新评论

发表评论