Monday, April 19, 2010

Creating List/Library Folders Programmatically

Very useful link to create folders programmatically:

I am calling createCT method just after the folder has created:



public static SPFolder GetFolder(SPList targetList, string folderUrl)


{


if (string.IsNullOrEmpty(folderUrl))


return targetList.RootFolder;


SPFolder folder = targetList.ParentWeb.GetFolder(targetList.RootFolder.Url + "/" + folderUrl);


if (!folder.Exists)


{


if (!targetList.EnableFolderCreation)


{


targetList.EnableFolderCreation = true;


targetList.Update();


}


// We couldn't find the folder so create it


string[] folders = folderUrl.Trim('/').Split('/');


string folderPath = string.Empty;


for (int i = 0; i < folders.Length; i++)


{


folderPath += "/" + folders[i];


folder = targetList.ParentWeb.GetFolder(targetList.RootFolder.Url + folderPath);


if (!folder.Exists)


{


SPListItem newFolder = targetList.Items.Add("", SPFileSystemObjectType.Folder, folderPath.Trim('/'));


newFolder.Update();


folder = newFolder.Folder;


}


}


createCT(folderUrl, folder, targetList);


}


// Still no folder so error out


if (folder == null)


throw new SPException(string.Format("The folder '{0}' could not be found.", folderUrl));


return folder;


}

Wednesday, April 14, 2010

creating a content type and attaching it to a Folder in Document Library and setting the metadata fields



public static void createCT(string Asset, SPFolder folder, SPList targetList)


{


SqlConnection cn = new SqlConnection("server=aaa;database=aaa;user id=sa;password=sa");


SqlCommand cmd = new SqlCommand("SELECT DISTINCT TBLATTLABEL.ATTLABELNAME, COMBINEDATTR.ATTRIBUTE, COMBINEDATTR.SEARCHVALUE, COMBINEDATTR.ISCI, COMBINEDATTR.IMAGEID, COMBINEDATTR.FORMATID FROM COMBINEDATTR INNER JOIN TBLATTLABEL ON TBLATTLABEL.ATTTABLESOURCE = 'Lookup_' + COMBINEDATTR.ATTRIBUTE WHERE ISCI ='" + Asset + "'AND IMAGEID = 0 ORDER BY ATTLABELNAME", cn);


cn.Open();


SqlDataReader dr = cmd.ExecuteReader();


using (SPSite p_site = new SPSite("http://localhost:10010/"))


{


using (SPWeb p_web = p_site.OpenWeb())


{


SPContentType CustomContentType = new


SPContentType(p_web.AvailableContentTypes["Folder"],


p_web.ContentTypes, Asset + "ContentType");


int i = 0;


while (dr.Read())


{


// A string Field


p_web.Fields.Add(dr[0].ToString(), SPFieldType.Text, false);


SPFieldLink fLink1 = new SPFieldLink(p_web.Fields[dr[i].ToString()]);


CustomContentType.FieldLinks.Add(fLink1);


}



p_web.ContentTypes.Add(CustomContentType);


CustomContentType.Update();


targetList.ContentTypes.Add(CustomContentType);


targetList.Update();


SPListItem item = folder.Item;


item["ContentTypeId"] = CustomContentType.Id;


item.Update();


dr.Close();


dr = cmd.ExecuteReader();


while (dr.Read())


{


item[dr[0].ToString()] = dr[2].ToString();


}


item.Update();


folder.Update();


}


}


}


Tuesday, April 13, 2010

Creating SharePoint VPC Image Requisites

1. Install Virtual PC 2007 with SP1
2. Install Windows Server 2008 Standard (32-bit)
3. Install Virtual Machine Additions
4. Install .NET Framework 3.5 SP1
5. Install SQL Server 2008 standard
6. Create MOSS Accounts for the MOSS install
7. Install MOSS 2007 Enterprise
8. Install Visual Studio 2008 Professional Edition
9. Install Visual Studio Extension for WSS3.0 V1.3
10. Install MOSS SDK v1.4(SharePoint Server 2007 SDK: Software Development Kit)
11. Configure SharePoint Central Administration Web Application, the URL is: http://localhost:10100/
12. Configure and started services(Windows SharePoint Services Search Service, Office SharePoint Server Search, Excel Calculation Services) on sharepoint farm
13. Create SharePoint Shared Service ,the URL is: http://localhost:1111/ssp/admin/default.aspx
14. Create a Web Application for MySites,the URL is: http://localhost:2222/
15. Configure the SSP Search settings with the Content sources and crawl schedules
16. Create a collaboration portal for Company with at defult port 80,the URL is: http://localhost/pages/default.aspx
17. Create a Team portal with port 10010, ,the URL is: http://localhost:10010/
18. Install Microsoft Office 2007
19. Install SharePoint Designer 2007
20. WSP Builder
21. Silverlight 3.0 Runtime
22. Visual Studio 2008 SP1
23. Visual Studio 2008 SP1 Tools for Silverlight
24. Silverlight 3.0 SDK
25. Modifying the SharePoint web.config file
26. Seting the correct MIME-type for the Silverlight .xap filetype in IIS

uploading Documents with meta data to SharePoint Doc lib Folder



public static void UploadFileWithMetaData(string Asset, string fileName)


{


SqlConnection cn = new SqlConnection("server=aaa;database=aaa;user id=sa;password=sa");


SqlCommand cmd = new SqlCommand("SELECT FORMAT2.FILENAME, FORMAT1.ISCI, FORMAT2.PATH+FORMAT2.FILENAME as FullPath, Format2.FileLocation,Format2.Format, Format2.RES FROM FORMAT as Format1 inner join Format as Format2 on Format1.ISCI=Format2.ISCI WHERE FORMAT1.FORMAT = 'HFD' AND Format2.Format <> 'DATA' AND FORMAT1.ISCI='" + Asset + "' AND FORMAT2.FILENAME='" + fileName + "'", cn);


cn.Open();


SqlDataReader dr = cmd.ExecuteReader();


SPSite siteCollection = new SPSite("http://localhost:10010/");


while (dr.Read())


{


using (SPWeb web = siteCollection.RootWeb)


{


SPList list = web.Lists["Video Library"];


SPFolder folder = GetFolder(list, dr[1].ToString());


if (dr[0].ToString() != String.Empty)


{


SPFileCollection fcol = folder.Files;


FileStream fstream = File.OpenRead(dr[2].ToString());


byte[] content = new byte[fstream.Length];


fstream.Read(content, 0, (int)fstream.Length);


fstream.Close();


string destFile = fcol.Folder.Url + "/" + dr[0].ToString();


SPFile addedFile = fcol.Add(destFile, content, false);


SPItem item = addedFile.Item;


if (dr[0].ToString() != String.Empty)


item["Name"] = dr[0].ToString();


if (dr[1].ToString() != String.Empty)


item["ISCI"] = dr[1].ToString();


if (dr[2].ToString() != String.Empty)


item["FullPath"] = dr[2].ToString();


if (dr[3].ToString() != String.Empty)


item["FileLocation"] = dr[3].ToString();


if (dr[4].ToString() != String.Empty)


item["Format"] = dr[4].ToString();


if (dr[5].ToString() != String.Empty)


item["RES"] = dr[5].ToString();


item.Update();


addedFile.Update();


}


}


}


cn.Close();


}