设为首页 收藏本站
查看: 603|回复: 0

[经验分享] python操作sharepoint对象模型

[复制链接]

尚未签到

发表于 2015-4-22 06:34:47 | 显示全部楼层 |阅读模式
前段时间刚接触python,发觉它很简单很实用。最近也一直做sharepoint的项目,很自然就想到,能不能使用python来做一些简单的sharepoint? 如果能直接操作sharepoint的对象模型,使用python对sharepoint做一些简单的开发定制应该是可行吧?
    于是花了点时间研究了一下,写一些代码。基本上我是在把对sharepoint对象模型操作封装成.net com对象,然后在python里通过pythonwin的com api操作这些对象。
    下面是代码:

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

namespace 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);
            }
        }

    }
}

PyList
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Runtime.InteropServices;

namespace 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();
        }
    }
}

PyListCollection
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Runtime.InteropServices;

namespace 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]);
        }

    }
}

PyListItem
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Runtime.InteropServices;

namespace 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();
        }
    }
}

PyListItemCollection
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Microsoft.SharePoint;
5 using System.Runtime.InteropServices;
6
7 namespace PySP
8 {
9     [ComVisible(true)]
10     public class PyListItemCollection
11     {
12         protected SPListItemCollection items = null;
13
14         protected void EnsureListItems()
15         {
16             if (items == null)
17                 throw new 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 int Count
29         {
30             get
31             {
32                 EnsureListItems();
33                 return this.items.Count;
34             }
35         }
36
37         public PyListItem GetItemByIndex(int index)
38         {
39             EnsureListItems();
40             return new PyListItem(this.items[index]);
41         }
42     }
43 }
44  编译后生成pyps.dll,然后注册com组件,运行命令regasm pyps.dll / register
  下面是python 代码:
  

pspy.py
  1 DSC0002.gif import win32com.client
  2class 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        return pyweb(self.__com__.openweb(url))
  8    def Close(self):
  9        self.__com__.close
10  
11
12class PyWeb(object):
13    def __init__(self,com):
14        self.__com__=com
15    @property
16    def AllowAnonymousAccess(self):
17        return self.__com__.AllowAnonymousAccess
18    @property
19    def AllowAutomaticASPXPageIndexing(self):
20        return self.__com__.AllowAnonymousAccess
21    @AllowAutomaticASPXPageIndexing.setter
22    def AllowAutomaticASPXPageIndexing(self,bool):
23        self.__com__.AllowAutomaticASPXPageIndexing=bool
24    @property
25    def AllowRssFeeds(self):
26        return self.__com__.AllowAnonymousAccess
27    @property
28    def AllowUnsafeUpdates(self):
29        return self.__com__.AllowAnonymousAccess
30    @AllowUnsafeUpdates.setter
31    def AllowUnsafeUpdates(self,value):
32        self.__com__.AllowUnsafeUpdates=value
33    @property
34    def Title(self):
35        return self.__com__.Title
36    @Title.setter
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        return pylistcollection(self.__com__.Lists)
46
47class PyList(object):
48    def __init__(self,com):
49        self.__com__=com
50    @property
51    def Title(self):
52        return self.__com__.Title
53    @Title.setter
54    def Title(self,value):
55        self.__com__.Title=value
56    @property
57    def AllowContentTypes(self):
58        return self.__com__.AllowContentTypes
59    @property
60    def AllowDeletion(self):
61        return self.__com__.AllowDeletion
62    @property
63    def CanReceiveEmail(self):
64        return self.__com__.CanReceiveEmail
65    @property
66    def Created(self):
67        return self.__com__.Created
68    @property
69    def Items(self):
70        return pylistitemcollection(self.__com__.GetAllItems)
71    def update(self):
72        self.__com__.Update
73
74class PyListCollection(object):
75    def __init__(self,com):
76        self.__com__=com
77    @property
78    def Count(self):
79        return self.__com__.Count
80    def getList(self,index=-1,name=''):
81        if index!=-1:
82            return pylist(self.__com__.GetListByIndex(index))
83        if name!='':
84            return pylist(self.__com__.GetListByName(name))
85
86class PyListItem(object):
87    def __init__(self,com):
88        self.__com__=com
89    @property
90    def Title(self):
91        return self.__com__.Title
92    def Update(self):
93        self.__com__.Update
94
95class PyListItemCollection(object):
96    def __init__(self,com):
97        self.__com__=com
98    @property
99    def Count(self):
100        return self.__com__.Count
101    def getItem(self,index=-1):
102        return  pylistitem(self.__com__.GetItemByIndex(index))
103   
104
105          
  最后是python的简单测试代码:
  >>site=PySite('http://sun/')
  >>web=site.OpenWeb('/test')
  >>print  web.Title
  >>'Moss'
  >>web.Title='MyMoss'
  >>web.Update()
  >>print web.Title
  >>'MyMoss'
  这是一时无聊写下的代码,本身也没有什么实用价值。
  不过还是希望大虾们指点一下,这样写会有什么副作用没有?
  谢谢.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-59326-1-1.html 上篇帖子: python的装饰器 下篇帖子: python开发_python关键字
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表