0210 : SP Lists
1.Creating a List by Using the Object Model
• SPSite class
• SPWeb class
• SPListCollection class
• SPList class
• SPView class
• SPField class
• SPListTemplate class
1.Creating a List by Using the Object Model
private void AddField(SPList newList, TextBox tb, DropDownList ddl)
{
SPView defaultView = newList.DefaultView;
newList.Fields.Add(tb.Text, GetFieldType(ddl), false);
SPField newField = newList.Fields[tb.Text];
defaultView.ViewFields.Add(newField);
defaultView.Update();
}
// Step 1: Get a handle to the site collection and web site
SPSite site = new SPSite(txtSiteUrl.Text);
SPWeb web = site.AllWebs[txtWebName.Text];
SPListCollection listCollection = web.Lists;
web.AllowUnsafeUpdates = true;
// Step 2: Create the new list
listCollection.Add(txtListName.Text, "",
web.ListTemplates[ddlListType.SelectedItem.Text]);
SPList newList = listCollection[txtListName.Text];
// Step 3: Add any user-defined fields
AddField(….)
web.Dispose();
site.Dispose();
}
2.Creating a List by Using Web Services
Web Service References
• http://[server name]/_vti_bin/Lists.asmx web service
• http://[server name]/_vti_bin/Views.asmx web service
• http://[server name]/_vti_bin/Webs.asmx web service
3.Updating a List by Using the Object Model
SPSite site = new SPSite("http://localhost");
SPWeb web = site.AllWebs[""];
SPList list = web.Lists["Employee"];
SPListItem item;
int ID;
lblReturnMsg.Text = "";
web.AllowUnsafeUpdates = true;
item = list.GetItemById(ID);
//change item
item["EmpName"] = txtEmpName.Text;
item.Update()
list.Update();
web.Dispose();
site.Dispose();
4.Updating a List by Using Web Services
http://[Server]/_vti_bin/Lists.asmx
ListService.GetListItems(…)
ListService.UpdateListItems.UpdateListItems(listName, xmlBatch)
5.Adding a Document to a Document Library by Using
the Object Model
// Step 1: Get handle to site collection, web site, list
SPSite site = new SPSite(txtSiteUrl.Text);
SPWeb web = site.AllWebs[txtWebName.Text];
SPList dl = web.Lists[txtDocLibName.Text];
SPFile file;
web.AllowUnsafeUpdates = true;
web.Lists.IncludeRootFolder = true;
// Step 2: Make sure user has selected a file
if (FileUpload1.PostedFile.FileName != "")
{
// Step 3: Load the content of the file into a byte array
Stream fStream;
Byte[] contents =
new Byte[FileUpload1.PostedFile.InputStream.Length];
fStream = FileUpload1.PostedFile.InputStream;
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
// Step 4: Upload the file to SharePoint doclib
file = web.Files.Add(web.Url.ToString() + "/" +
dl.Title.ToString() + "/" +
FileUpload1.FileName, contents);
file.Update();
}
else
{
lblErrorMsg.Text = "Please select a file to upload";
lblErrorMsg.Visible = true;
}
web.Dispose();
site.Dispose();
6.Adding a Document to a Document Library by Using
Web Services
Need to modify web.config (<httpRuntime maxRequestLength="512000"/>)if planning to upload file more than 4MB
might need to change connection time out value in IIS to enable connections toopen longer for slow connections or large file uploads
Recipe—C#: FileUploadService (See Project FileUploadService, Class Service.cs)
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string UploadFile2SharePoint(
string fileName, byte[] fileContents, string siteUrl,
string webName, string pathFolder)
{
// Step 1: Make sure a valid file has been passed
// to the service method
if (fileContents == null)
{
return "Missing File";
}
try
{
// Step 2: Open the target site and web
SPSite site = new SPSite(siteUrl);
SPWeb web = site.AllWebs[webName];
// Step 3: Open the folder to hold the document
SPFolder folder =
web.GetFolder(
EnsureParentFolder(web,pathFolder+"/"+fileName));
bool boolOverwrite = true;
// Step 4: Add the file
SPFile file = folder.Files.Add(
fileName, fileContents, boolOverwrite);
web.Dispose();
site.Dispose();
// Step 5: Declare victory!
return "'" + file.Name + "' successfully written to '" +
file.Item.Url + "'";
}
catch (System.Exception ex)
{
return ex.Message;
}
}
0 Comments:
Post a Comment
<< Home