前段时间刚接触python,发觉它很简单很实用。最近也一直做sharepoint的项目,很自然就想到,能不能使用python来做一些简单的sharepoint? 如果能直接操作sharepoint的对象模型,使用python对sharepoint做一些简单的开发定制应该是可行吧?
于是花了点时间研究了一下,写一些代码。基本上我是在把对sharepoint对象模型操作封装成.net com对象,然后在python里通过pythonwin的com api操作这些对象。
下面是代码:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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);
}
}
}
}
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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();
}
}
}
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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]);
}
}
}
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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();
}
}
}
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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 代码:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
最后是python的简单测试代码:
>>site=PySite('http://sun/')
>>web=site.OpenWeb('/test')
>>print web.Title
>>'Moss'
>>web.Title='MyMoss'
>>web.Update()
>>print web.Title
>>'MyMoss'
这是一时无聊写下的代码,本身也没有什么实用价值。
不过还是希望大虾们指点一下,这样写会有什么副作用没有?
谢谢.