PS:自己初学C#,SharePoint时做的一个小tool。
Friday, November 28, 2014
这个tool编译出来以后可以把部门的daily report保存到本地,数据库,和SharePoint的daily report list中,经过测试能够在我本地的SharePoint2010 SP1环境上正常运行了。
学习C#的第二个小程序,过程中遇到了好多问题,不是很完善,就是意思意思,本次程序练习提取窗体中的信息;向数据库中建表,判断并插入数据;把文件保存到本地指定的路径下;把内容在SharePoint指定list中保存成item并为列赋值。
在此记录一下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.SharePoint;
using System.Globalization;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SaveToLocalButton_Click(object sender, EventArgs e)
{
SaveToLocal();
}
private void SaveToSQLButton_Click(object sender, EventArgs e)
{
SaveToSQL();
}
private void SaveToWebButton_Click(object sender, EventArgs e)
{
try
{
SaveToWeb();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void SaveToAllButton_Click(object sender, EventArgs e)
{
SaveToLocal();
SaveToSQL();
SaveToWeb();
}
private void SaveToLocal()
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
string path = fbd.SelectedPath;
MessageBox.Show("Your daily reort will be saved under the path:" + path);
string filename = "DailyReport.doc";
DailyReportText.SaveFile(@path + "/" + filename, RichTextBoxStreamType.TextTextOleObjs);
}
}
private void SaveToSQL()
{
string connectString = @"Data Source=WIN-VVBGFRT65MV\ALWAYSON2;initial catalog=master;uid=sa;pwd=1qaz2wsxE;integrated security=true;pooling=false";
SqlConnection myConnect = new SqlConnection(connectString);
myConnect.Open();
SqlCommand myCommand = myConnect.CreateCommand();
string dateLine = DailyReportText.Lines[0];
int index = dateLine.IndexOf(":");
int lastIndex = dateLine.Length-1;
string date = dateLine.Substring(index+1, lastIndex-index);
myCommand.CommandText =
"use master if exists(select * from dbo.SysObjects where name='DailyReportDB') begin insert into DailyReportDB(date,content) values('"
+ date + "','" + DailyReportText.Text.ToString()+
"') end else begin create table DailyReportDB(date varchar(1000) primary key,content varchar(1000)) end";
try
{
SqlDataReader myDataReader = myCommand.ExecuteReader();
myDataReader.Close();
}
catch (SqlException)
{
MessageBox.Show("The report of the date you input has already existed in database!");
}
myConnect.Close();
}
private void SaveToWeb()
{
try
{
using (SPSite qaSite = new SPSite("http://win-vvbgfrt65mv"))
{
using (SPWeb qaWeb = qaSite.OpenWeb("/"))
{
string listURL = "http://win-vvbgfrt65mv/Lists/DailyReport";
SPList DailyReportList = qaWeb.GetList(listURL);
SPListItem item = DailyReportList.Items.Add();
//Get the date and time from the form
string dateLine = DailyReportText.Lines[0];
int index = dateLine.IndexOf(":");
int lastIndex = dateLine.Length-1;
string date = dateLine.Substring(index+1, lastIndex-index);
//Get the created by
SPUser currentUser = qaWeb.CurrentUser;
//Take the value into colume
string DataAndTime = date.ToString();
string Description = DailyReportText.Text.ToString();
try
{
string dateFormat = "yyyy/MM/dd";
DateTime SPdate = DateTime.ParseExact(date, dateFormat, CultureInfo.CurrentCulture, DateTimeStyles.None);
//ToShortDateString is important for format
item["Date and Time"] = SPdate.ToShortDateString();
}
catch (Exception)
{
MessageBox.Show("TimeFormat is not right!");
}
try
{
item["Description"] = Description;
}
catch (Exception)
{
MessageBox.Show("Description is not right!");
}
item["Created By"] = currentUser;
try
{
item.Update();
}
catch (Exception ex)
{
MessageBox.Show("Item update Exception!");
MessageBox.Show(ex.ToString());
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
附上一张SharePoint中生成item的图片: