[Home] [Recent] [Site Map] [SharePoint] [XBOX]
Wow, Nick and Steve are really stepping things up! I"m seriously looking at how I might get along to all these meetings...
Recently, I had to build the following application: pdf documents were generated automatically from an external application and associated with a set of text files containing their meta-data. The main goal was to upload those documents into a SharePoint document list, with there meta-data extracted from the text file. Then a workflow is invoked for approbation with several parameters depending on meta-data.
In this article I"ll focus on importing files into the list with their meta-data.
The first thought that came to my mind was about uploading one document into the list, then adding meta-data one by one to their corresponding columns. But because I have to manage versions for this list, this solution seems not adequate: one update for the document and one update for the meta-data means two versions instead of one.
But I"ve found one overloading really interesting.
First, let"s consider the list like a folder. And inside this folder we have a collection of files. If I add a file to this collection, this is the same as uploading a file, no ?
The folder name is the internal name of the list. The internal name is the name you first set for the list. For instance, if you have created a Purchase Order list with the name PurchaseOrder, then you have modified the name to Purchase Order, the folder name will be PurchaseOrder. You can check it with the url, i.e. http://dcmoss:50010/testgy/PurchaseOrder/ where PurchaseOrder is the name of the folder.
SPFolder folder = _web.GetFolder("internal list name");
SPFileCollection files = folder.Files;
Then I get the file itself, storing it in a memory stream
FileStream fStream = File.OpenRead("fileName");
And, at last, I create the URL of the file, with the file name
string url = "url web site" + "internal list name" + "/" + Path.GetFileName("fileName");
Meta-data have to be stored into a Hashtable
HashTable MetaDataTable = new HashTable();
MetaDataTable.Add("column name", "value");
In fact, I have a class which parse the text file and send me back a Hashtable. Then I add everything to the file collection
SPFile currentFile = files.Add(url, fStream, MetaDataTable, true);
The boolean value allow you to replace a file if it is already existing
In one line, we add the file with its meta-data ! Not bad.
Here is an example of code for adding a document with its meta-data
//Using required, especially System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
namespace DocImport
{
public class DocManagement {
//Some variables
private string _DestUrlPath = string.Empty;
private string _DestFolder = string.Empty;
private SPSite _site = null;
private SPWeb _web = null;
public DocManagment(string DestUrl, string DestFolder) {
_DestUrlPath = DestUrl;
_DestFolder = DestFolder;
_site = new SPSite(_DestUrlPath);
_web = _site.OpenWeb();
}
private bool _uploadDocument(string pdfFile, string txtFile) {
try {
//This is mandatory for avoiding an error
_web.AllowUnsafeUpdates = true;
//Use the list as a folder
SPFolder folder = _web.GetFolder(_DestFolder);
SPFileCollection files = folder.Files;
//Get the file
FileStream fStream = File.OpenRead(pdfFile);
HashTable MetaDataTable = new HashTable();
MetaDataTable.Add("nom de colonne", "valeur");
//Set the destination url for SharePoint
string url = _DestUrlPath + _DestFolder + "/" + Path.GetFileName(pdfFile);
//Add document to the list with metadata, and overwrite an existing document
with the same name
SPFile currentFile = files.Add(url, fStream, MetaDataTable, true);
fStream.Close();
return true;
} catch (Exception x) {
throw new Exception(x.Message);
}
}
}
Just discovered a little annoyance in naming managed properties for search scopes that was not immediately obvious as to its cause.
If you get the error
"The settings could not be saved because of an internal error: name"
after you try to name a new managed property, you probably have named it using an underscore or space in the name. Remove all underscores and spaces and it will be happy. (there may well be other special characters it does not like either)
Hi,
Welcome to my new blog. This blog will mainly be concerned around SharePoint-related stuff, quirks and findings :-)
So without further ado I bid you welcome.
Kind regards
anchorPoint
In a solution I"m building I was met with the task of creating a web site programmaticly and no sweat I thought. The site creation itself was a breeze, but I wsa met a bit of a challange when trying to create navigation tabs.
The parent web site would have a tab on the top navigation bar pointing to the new web site, and the newly created web site would have a tab as well that was highlighted in the top navigation when the user was visiting the site.
No problem I thought again. So I happily went and created the tab to the new site on the parent web site and told the newly created website to just inherit navigation. But to my surprise the tab on the newly created site was not highlighted when I was visiting it. This kinda puzzled me.
I had told the top navigation bar of the parent web site to use this node:
SPNavigationNode navigationNode = new SPNavigationNode("My Web Site", "/MyWebSite");
But apparently that wasn"t enough to highlight the tab.
After some more efford I finally figured out that it had to be:
SPNavigationNode navigationNode = new SPNavigationNode("My Web Site", "/MyWebSite/default.aspx");
Yeah .. you have to remember default.aspx. Anyway the code to create a child web site with parent top navigation and tab highlighting would be:
using (SPSite site = new SPSite(siteUrl)){
using (SPWeb web = site.AllWebs.Add("/MyWebSite", "My Web Site", "", 1033, "STS#1", false, false)){
SPNavigationNode navigationNode = new SPNavigationNode("My Web Site", "/MyWebSite/default.aspx"); SPNavigation navigation = web.ParentWeb.Navigation; SPNavigationNodeCollection topNavigationNodeCollection = navigation.TopNavigationBar;topNavigationNodeCollection.AddAsLast(navigationNode);
web.Navigation.UseShared =
true;web.Update();
}
}
Happy SharePoint hacking.
Very cool map to explore the Microsoft office features and it is a WPF application ;)
you can download it from "Microsoft Office Interactive Developer Map"
The Microsoft Office Interactive Developer Map is a Windows Presentation Foundation (WPF) application that helps developers visualize the different programs, servers, services, and tools that will help them build solutions. It allows them to drill down to each product and technology and learn about new features, objects, Web services, namespaces, and schemas required to extend Microsoft Office and build custom Office Business Applications (OBAs).
Note: I have used Windows live writer beta 2 for writing this post and it is very cool
Here is some reference material available for the OpenDocuments ActiveX control
Here is the OpenDocuments ActiveX Control documentation available on msdn, which was barely updated for wssv3, see my comments under Community Content. This documentation explains how to connect your OpenDocuments Control to SharePoint.
For wss v2, there was the SharePad Reference Application for SharePoint Products and Technologies which Mike Fitzmaurice [MSFT] talked about it here. It is on GotDotNet which is currently being phased out, so this link may break in the near future.
But for wss v3, this application was not been updated. Not sure why. One would think that Microsoft would want third parties to integrate these files w/ the SharePoint environment, especially moving forward with SharePoint 2007.
--Tom
Site List:
>>Xbox Live_s Major Nelson
>>Xbox 360 & SharePoint 2007 Weblog
>>Carsten Keutmann_s Blog
>>Mohamed Zaki_s Blog [Sharepoint MVP]
>>The Mit_s Blog
>>Mart Muller_s Sharepoint Weblog
>>Microsoft SharePoint Products and Technologies Team Blog
>>SharePoint Solutions Blog
>>4GuysFromRolla.com Headlines
>>ASP.NET Blogs
>>SharePoint Blogs
>>SharePoint Blogs
>>Joel on Software
>>ADO Guy_s Rants and Raves
>>Microsoft Live Labs
>>GadgetNews
>>Windows Vista Team Blog
>>VoIP & Gadgets Blog
>>schrankmonster blog
>>Via Virtual Earth Blog
>>Feed
>>MSDN Blogs
>>Mashable!
Links:
Jack's Readings
Month Archives:
Oct 2007
Sep 2007
Top Tags:
social software social networking .NET mashable Sharepoint ASP.NET Web 2.0 Web2.0 Startups Community News Search Marketplace General Software Development AJAX Windows Vista Visual Studio Microsoft myspace Silverlight People Powered! YouTube Vista MOSS Featured News C# Events MOSS 2007 Google WPF Office 2007 Web Community Security General Personal Xbox 360 facebook Tools development SharePoint 2007 Fun Atlas Architecture ASP.NET AJAX myspace codes TheLongTail IIS SQL Server Developers Revenue Sharing Video Pictures WCF Mobile 2.0 Announcements Orcas MIX07 Arcade Team System JavaScript News
@2007 All rights Reserved |