Skip to content

Instantly share code, notes, and snippets.

@mdnmdn
Created December 19, 2011 09:14
Show Gist options
  • Save mdnmdn/1496243 to your computer and use it in GitHub Desktop.
Save mdnmdn/1496243 to your computer and use it in GitHub Desktop.
ironruby sharepoint access
require 'Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'
require 'Microsoft.SharePoint.Security, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'
include Microsoft::SharePoint
include Microsoft::SharePoint::Administration
class SPManager
attr_reader :site_collection_url
def initialize url
@site_collection_url = url
end
def site_collection
@site_collection ||= SPSite.new @site_collection_url
end
def site name
end
def close
@sites.values.each {|site| site.Dispose} if @sites
@site_collection.Dispose if @site_collection
@sites = {}
@_site_collection = nil
end
def all_users
site_collection.RootWeb.AllUsers.map {|u| User.new u}
end
end
class User
attr_accessor :name ,:login, :email , :groups
def initialize spuser = nil
if spuser
@name = spuser.name
@login = spuser.login_name
@email = spuser.email
@groups = spuser.Groups.map { | g | Group.new g}
end
end
def to_s
"#{@name} - #{@login} - #{@email}"
end
end
class Group
attr_accessor :name, :id
def initialize spgroup = nil
if spgroup
@name = spgroup.name
@id = spgroup.ID
end
end
def to_s
"#{@id}.#{@name}"
end
end
class SPUser
def to_hash
{:name => self.name,
:login => self.login_name,
:email => self.email}
end
end
$sp_url = 'http://localhost'
def site_collection
spm = SPManager.new $sp_url
spm.site_collection
end
def show_users users
users.each do |u|
p "#{u.name} (#{u.login})"
p " -> " + u.groups.map { |g| g.name }.join(', ')
nil
end
end
def show_user_groups web
web.Groups.each do |g|
p "" + g.name
g.Users.each {| u | p ' -> ' + u.name}
nil
end
end
spm = SPManager.new $sp_url
show_users spm.all_users
#show_user_groups spm.site_collection.RootWeb
spm.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment