How to use an MCMS Web service to import multiple items into the resource gallery (327750)



The information in this article applies to:

  • Microsoft Content Management Server 2002

This article was previously published under Q327750

SUMMARY

You can use the Web Author to administer resource gallery items in Microsoft Content Management Server (MCMS) 2002. However, if you must add multiple items to the resource gallery, this may be inconvenient. This article describes how to create a MCMS Web service to perform bulk imports of resources into the resource gallery.

MORE INFORMATION

The following sample Web service shows how to import multiple resources into the gallery. Note that the sample is not a ready-to-use solution, although it will work in most scenarios. The sample only focuses on the functionality and does not include error handling routines. You can adjust the sample to your specific environment.

To create the MCMS Web service, follow these steps:
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, expand Content Management Server Projects under Project Types, and then select Visual C# Projects.
  4. Under Templates, click MCMS Web Service to create the project.
  5. On the File menu, point to Add New Item. Under Categories, expand Web Project Items, and then select Code.
  6. Under Templates, click Web Service to add a new Web service to your project.
  7. In the new .asmx file, switch to Code view. Replace the existing code with the following:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    using System.Security.Principal;
    using System.IO;
    using Microsoft.ContentManagement.Publishing;
    
    
    namespace ResourceManager
    {
    	/// <summary>
    	/// Summary description for AddResources.
    	/// </summary>
    	public class AddResources : System.Web.Services.WebService
    	{
    		public AddResources()
    		{
    			//CODEGEN: This call is required by the ASP.NET Web Services Designer.
    			InitializeComponent();
    		}
    
    		#region Component Designer generated code
    		
    		//Required by the Web Services Designer.
    		private IContainer components = null;
    				
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    		}
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if(disposing && components != null)
    			{
    				components.Dispose();
    			}
    			base.Dispose(disposing);		
    		}
    		
    		#endregion
    
    		[WebMethod]
    		public string DoAddResources(string ResourceGalleryPath, string PathContainingResource)
    		{
    			// Get the MCMS application context.
    			CmsApplicationContext cmsContext = new CmsApplicationContext();
    
    			// Logon to MCMS:
    			// Assumes that you have Windows Authentication turned on.
    			WindowsIdentity ident = HttpContext.Current.User.Identity as WindowsIdentity;
     
    			// Make sure to set the context to Update mode (which means you can write 
    			// back to MCMS using PAPI).
    			cmsContext.AuthenticateUsingUserHandle(ident.Token.ToInt32(),PublishingMode.Update);
    
    			ResourceGallery rsg = cmsContext.Searches.GetByPath(ResourceGalleryPath) as ResourceGallery;
    
    			//Import all items in a particular directory.
    			DirectoryInfo dir = new DirectoryInfo( PathContainingResource );
    			int i=0;
    			foreach(FileSystemInfo fsi in dir.GetFileSystemInfos()) 
    			{
    				FileInfo f = (FileInfo)fsi;
    				rsg.CreateResource(f.FullName);
    				i++;
    			}
    
    			// Commit changes back to MCMS.
    			cmsContext.CommitAll();
     
    			return i.ToString()+" Items added to the resource gallery "+ResourceGalleryPath;
    		}
    	}
    }
    					
  8. Build the solution.
  9. Open the Internet Service Manager (ISM) and disable anonymous access for the virtual directory that contains the new Web service. You must do this because the service will access the MCMS database with the account of the currently connected user, and the currently connected user must be an MCMS administrator.
  10. Open Microsoft Internet Explorer and use the following URL to open the Web service location (note that you must substitute web service location and service name with the names in your project):

    http://localhost/web service location/service name.asmx

  11. In the browser, click the DoAddResource method.
  12. In the first input field, type the destination path in the resource gallery. This resource gallery must already exist. The path that is specified is the same as the path that is shown in the Site Deployment Manager. The path must be an absolute path and must start with "/Resources/". For example, to add resource gallery items to the Sample Small Business Gallery, specify /Resources/Sample/Small Business.
  13. In the second input field, specify the source directory where the items that you want to import to the database are located.

    NOTE: All files in this directory will be uploaded to the database. This directory must be located on the Web server computer. If it is not, delegation issues may occur.

Modification Type:MajorLast Reviewed:5/6/2005
Keywords:kbfix kbhowto KB327750