Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

File Metadata

  Asked By: Michael    Date: Jul 31    Category: Sharepoint    Views: 2062

I have a problem. I want to know how can we extract file
metadata information of any documents (*.doc,*.ppt,*.xls,*.pdf ...)
while uploading the file in a document library in SharePoint,
programatically?

Let me explain. I am trying to upload files in a document
library of a SharePoint site using .NET (C#) code. I made use of
ADO.NET features to write my sql queries and input the values (WebID,
SiteID etc) in the database for the file needed to get uploaded.

The file is getting uploaded succesfully but its property
is getting lost. The icon changes (not showing word symbol for word
documents, powerpoint for ppt etc) and the file opens
with "Open", "Save" etc options which should not be the normal case.

I checked that the metainfo and metainfosize of these files
are not getting inserted and updated in the database (SharePoint's
default content database). I am using SQL Server 2000 SP3. Can anyone
suggest me as to what should I do and where the problem lies?

I was thinking to extract the file metadata information,
calculate the size and dump it into the database using the same C#
code, but alas there are no articles, till now that I could discover.
Can anyone suggest me some solutions (Better will be the articles or
best will be the source code)?

By the way, I am using Components to make my task easier.

This is my code:

Component code

using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;

namespace ClassLibrary
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}

public void database_read()
{
// Connection String
const string con_str = "Data
Source=192.168.0.47; Initial Catalog=wss_content; Integrated
Security=SSPI";
byte[] b = new byte[5000];

try
{
FileStream stream =
System.IO.File.OpenWrite("D:\\metainfo.txt");

SqlConnection con = new SqlConnection
(con_str);
SqlCommand cmd = new SqlCommand
("Select MetaInfo from Docs where Id ='{E3932526-7E27-4EE0-954A-
4E6272C3CA7A}'",con);
con.Open();

SqlDataReader r;
r= cmd.ExecuteReader();

if (r.HasRows)
{
while (Convert.ToBoolean
(r.Read()) != false)
{
r.GetBytes
(0,0,b,0,b.Length);
}
}

stream.Write(b,0,b.Length);

stream.Close();
con.Close();
}
catch(System.Exception e){}


// System.IO.StreamWriter sw = new StreamWriter
("D:\\metainfo.txt");
// sw.Write(str);
// sw.Close();

// TextWriter tw = new StreamWriter
("D:\\metainfo.txt");

// write a line of text to the file
// tw.Write(str);

// close the stream
// tw.Close();
}

public void database_in(string path)
{
try
{
// Connection String
const string con = "Data
Source=192.168.0.47; Initial Catalog=wss_content; Integrated
Security=SSPI";
int len;

FileStream stream =
System.IO.File.OpenRead(path);
byte [] content = new byte
[stream.Length];
stream.Read(content,0,(int)
stream.Length);
System.IO.FileAttributes attr =
File.GetAttributes(path);
len = attr.ToString().Length;

System.Data.SqlClient.SqlConnection conn = new
System.Data.SqlClient.SqlConnection(con);
System.Data.SqlClient.SqlCommand
com = new System.Data.SqlClient.SqlCommand("file_input",conn);

conn.Open();

com.Parameters.Add
("@SiteId","{B8962309-8EDC-428F-978C-0871D6E29508}" );
com.Parameters.Add("@DirName","New Library1");
com.Parameters.Add
("@LeafName",System.IO.Path.GetFileName(path));
com.Parameters.Add("@WebId","{D5B798A8-23F7-4A30-BD78-
794EB3C1DBD5}");
com.Parameters.Add("@ListId","{4809660E-7283-4CF1-
88DF-EC6FA848DC47}");
com.Parameters.Add
("@DoclibRowId","30");
com.Parameters.Add("@Content",content);
com.Parameters.Add
("@Size",content.Length);
// com.Parameters.Add
("@MetaInfo",File.GetAttributes(path));
com.Parameters.Add
("@MetaInfoSize",len);
com.CommandType =
CommandType.StoredProcedure;

// str = File.GetAttributes
(path).ToString();
// len = File.GetAttributes
(path).ToString().Length;
// str1= "File Attributes : " + str + " with length " +
len.ToString();

com.ExecuteNonQuery();

conn.Close();
stream.Close();
}
catch (System.Exception exp){}
}
}
}

ASPX Code:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="File_Upload.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1"
name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript"
name="vs_defaultClientScript">
<meta
content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post"
encType="multipart/form-data" runat="server">
<INPUT id="File1" style="Z-INDEX: 101; LEFT:
40px; POSITION: absolute; TOP: 40px" type="file"
name="File1" runat="server">
<asp:button id="cmdUpload" style="Z-INDEX:
102; LEFT: 288px; POSITION: absolute; TOP: 40px"
runat="server"
Text="Upload"></asp:button><asp:label id="lblMessage" style="Z-INDEX:
103; LEFT: 48px; POSITION: absolute; TOP: 112px"
runat="server" Height="8px"
Width="184px"></asp:label></form>
</body>
</HTML>


ASPX.CS Code:

using System;
using System.IO;
using System.Web;
using System.Data;
using ClassLibrary;
using System.Web.UI;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace File_Upload
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile
File1;
protected System.Web.UI.WebControls.Button cmdUpload;
string path = "D:\\Documents and
Settings\\debaditya.g\\Desktop\\Uploads\\";
protected System.Web.UI.WebControls.Label lblMessage;
long lMaxFileSize = 4096;


private void Page_Load(object sender,
System.EventArgs e)
{

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the
ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not
modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmdUpload.Click += new
System.EventHandler(this.cmdUpload_Click);
this.Load += new System.EventHandler
(this.Page_Load);

}
#endregion

private void DeleteFile (string strFileName)
{//Delete file from the server
if (strFileName.Trim().Length > 0)
{
FileInfo fi = new FileInfo
(strFileName);
if (fi.Exists)//if file exists delete
it
{
fi.Delete();
}
}
}

private void cmdUpload_Click(object sender,
System.EventArgs e)
{
// if (( File1.PostedFile != null) &&
(File1.PostedFile.ContentLength > 0))
// {
// //determine file name
// string sFileName =
System.IO.Path.GetFileName(File1.PostedFile.FileName);
// try
// {
// if
(File1.PostedFile.ContentLength <= lMaxFileSize)
// {
// //Save File on disk
//
File1.PostedFile.SaveAs(path + sFileName);
//
lblMessage.Visible=true;
//
lblMessage.Text="File: " + path + sFileName + " Uploaded
Successfully";
// }
// else //reject file
// {
//
lblMessage.Visible=true;
// lblMessage.Text="File
Size if Over the Limit of " + lMaxFileSize ;
// }
// }
// catch(Exception)//in case of an error
// {
// lblMessage.Visible = true;
// lblMessage.Text="An Error
Occured. Please Try Again!";
// DeleteFile(path + sFileName);
// }
// }

ClassLibrary.Class1 file_in = new
ClassLibrary.Class1();
// file_in.database_in(File1.Value);
file_in.database_read();

// Class1 file_input = new Class1();
// file_input.database_in();

lblMessage.Text = " File Uploaded ";
}
}
}


Faster reponse will be highly appreciated.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Michelle White     Answered On: Jul 31

The approach you took was wrong.

I would not suggest you to directly touch DB of any product.
Now SPS/WSS has a very extensive object model to support the activity you are
doing. you do not have to go to SQL server  fo that.
Just look at the classes
SPListCollection
SPList
SPFile

And all your problem  will be solved.

You need not and sould not program at the database  level.

However I must say the program is really good.

I will try and send you some of the code  snippets. if I an create any.

 
Didn't find what you were looking for? Find more on File Metadata Or get search suggestion and latest updates.




Tagged: