diff --git a/README.md b/README.md index c1081c0..608817e 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,18 @@ taskl.modified tasks = taskl.items ``` +### Retrieving Items with View fields +```ruby +# Retrieves an Array of Items with fields & it's values +list_name = 'ListOfProjects' +view_name = scli.get_view_collection(list_name).first.name +view = scli.get_view(list_name, view_name) +scli.get_list_items('ListOfProjects', :view_name => view_name, :view_fields => view.view_fields).each do |i| + fields_hash = i.fields + .... +end +``` + ### Creating/Renaming/Deleting a Task (other types of ListItems are forthcoming) ```ruby t1 = taskl.add_item!(:title => "New Task") diff --git a/lib/viewpoint/spws.rb b/lib/viewpoint/spws.rb index 22e84a2..3f4b7b9 100644 --- a/lib/viewpoint/spws.rb +++ b/lib/viewpoint/spws.rb @@ -52,11 +52,13 @@ def self.set_log_level(level) require 'viewpoint/spws/websvc/copy' # Lists Web Service require 'viewpoint/spws/websvc/lists' +require 'viewpoint/spws/websvc/views' require 'viewpoint/spws/types' require 'viewpoint/spws/types/list' require 'viewpoint/spws/types/tasks_list' require 'viewpoint/spws/types/document_library' require 'viewpoint/spws/types/list_item' +require 'viewpoint/spws/types/view' # User and Groups Web Service require 'viewpoint/spws/websvc/user_group' require 'viewpoint/spws/types/user' diff --git a/lib/viewpoint/spws/spws_client.rb b/lib/viewpoint/spws/spws_client.rb index 40999a0..438df83 100644 --- a/lib/viewpoint/spws/spws_client.rb +++ b/lib/viewpoint/spws/spws_client.rb @@ -44,6 +44,11 @@ def lists_ws @listsws ||= Websvc::Lists.new(@con) end + def views_ws + @views ||= Websvc::Views.new(@con) + end + + def usergroup_ws @usergroupws ||= Websvc::UserGroup.new(@con) end @@ -73,6 +78,11 @@ def get_lists lists_ws.get_list_collection end + # Retrieve all of the viewable list_items for the list. + def get_list_items(list, opts = {}) + lists_ws.get_list_items(list, opts) + end + # Retrieve a List object # @param [String] list title or the GUID for the list def get_list(list) @@ -108,4 +118,14 @@ def get_user(user) end usergroup_ws.get_user_info user end + + # ========= Views Accessor Proxy Methods ========= + def get_view_collection(list_name) + views_ws.get_view_collection(list_name) + end + + def get_view(list_name, view_name) + views_ws.get_view(list_name, view_name) + end + end diff --git a/lib/viewpoint/spws/types/list_item.rb b/lib/viewpoint/spws/types/list_item.rb index 8376fd3..2d9e251 100644 --- a/lib/viewpoint/spws/types/list_item.rb +++ b/lib/viewpoint/spws/types/list_item.rb @@ -26,6 +26,7 @@ class Viewpoint::SPWS::Types::ListItem attr_reader :title, :link_title, :status, :priority, :percent_complete attr_reader :start_date, :end_date, :recurrence, :all_day_event attr_reader :attachments + attr_reader :fields # @param [Viewpoint::SPWS::Websvc::List] ws The webservice instance this ListItem spawned from # @param [String] list_id The list id that this item belongs to @@ -235,6 +236,13 @@ def parse_xml_fields(xml) set_field :@attachments, 'ows_Attachments', @xmldoc, true set_field :@created_date, 'ows_Created_x0020_Date' unless @created_date set_field :@modified_date, 'ows_Last_x0020_Modified' unless @modified_date + begin + @fields = {} + xml.attributes.each do |a| + (@fields[a[1].name.gsub(/^ows_/, '')] = a[1].value rescue true) + end + rescue true + end @xmldoc = nil end diff --git a/lib/viewpoint/spws/types/view.rb b/lib/viewpoint/spws/types/view.rb new file mode 100644 index 0000000..069821d --- /dev/null +++ b/lib/viewpoint/spws/types/view.rb @@ -0,0 +1,28 @@ +class Viewpoint::SPWS::Types::View + include Viewpoint::SPWS::Types + + attr_reader :name, :default_view, :mobile_view, :mobile_default_view, :type + attr_reader :display_name, :url, :level, :base_viewID, :content_typeID, :imageUrl + attr_reader :view_fields + + def initialize(ws, xml) + @ws = ws + @view_fields = nil + parse_xml_fields(xml) + ns = {"xmlns" => xml.namespace.href} + xml.xpath('//xmlns:ViewFields/xmlns:FieldRef', ns).each do |l| + @view_fields ||= [] + @view_fields << l.attributes['Name'].value + end + end + + def parse_xml_fields(xml) + [:name, :default_view, :mobile_view, :mobile_default_view, :type, + :display_name, :url, :level, :base_viewID, :content_typeID, :imageUrl].each do |a| + # var = "@#{a}".to_sym + # val = xml[a.to_s.camel_case] + # puts "#{var.inspect} -> #{val.inspect}" + instance_variable_set "@#{a}".to_sym, xml[a.to_s.camel_case] + end + end +end diff --git a/lib/viewpoint/spws/websvc/views.rb b/lib/viewpoint/spws/websvc/views.rb new file mode 100644 index 0000000..7956977 --- /dev/null +++ b/lib/viewpoint/spws/websvc/views.rb @@ -0,0 +1,65 @@ +=begin + This file is part of ViewpointSPWS; the Ruby library for Microsoft Sharepoint Web Services. + + Copyright © 2011 Dan Wanek + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +=end + +# This class represents the Sharepoint Lists Web Service. +# @see http://msdn.microsoft.com/en-us/library/ms774654(v=office.12).aspx +class Viewpoint::SPWS::Websvc::Views + include Viewpoint::SPWS::Websvc::WebServiceBase + + def initialize(spcon) + @default_ns = 'http://schemas.microsoft.com/sharepoint/soap/' + @ws_endpoint = '_vti_bin/Views.asmx' + super + end + + def get_view_collection(listName) + soapmsg = build_soap_envelope do |type, builder| + if(type == :header) + else + builder.GetViewCollection { + builder.parent.default_namespace = @default_ns + builder.listName(listName) + } + end + end + soaprsp = Nokogiri::XML(send_soap_request(soapmsg.doc.to_xml)) + ns = {"xmlns"=> @default_ns} + views = [] + soaprsp.xpath('//xmlns:GetViewCollectionResponse/xmlns:GetViewCollectionResult/xmlns:Views/xmlns:View', ns).each do |l| + views << Types::View.new(self, l) + end + views + end + + def get_view(listName, viewName) + soapmsg = build_soap_envelope do |type, builder| + if(type == :header) + else + builder.GetView { + builder.parent.default_namespace = @default_ns + builder.listName(listName) + builder.viewName(viewName) + } + end + end + soaprsp = Nokogiri::XML(send_soap_request(soapmsg.doc.to_xml)) + ns = {"xmlns"=> @default_ns} + Types::View.new(self, soaprsp.xpath('//xmlns:GetViewResponse/xmlns:GetViewResult/xmlns:View', ns).first) + end + +end