Showing posts with label MOSS. Show all posts
Showing posts with label MOSS. Show all posts

Sunday, November 23, 2008

How to upload document from one document library into another document library

public void UploadChart()
{
try
{
String GenerateNo=”ABC_0001”;
String Vertical= Request.QueryString["Vertical"];
sourceWeb = mySite.OpenWeb();
destinationWeb = mySite.AllWebs[Vertical];
destinationWeb.AllowUnsafeUpdates = true;
destinationWeb.Update();
sourceDocuments = sourceWeb.Lists["List Template Gallery"];
destinationDocuments = destinationWeb.Lists["Chart"];
currentSourceDocument = sourceDocuments.Items.GetItemById(25);
fileBytes = currentSourceDocument.File.OpenBinary();
fileNameandExtn = currentSourceDocument.File.Name.Split(".".ToCharArray());
NameofDocument = fileNameandExtn[0] + "_" + GenerateNo + "." + fileNameandExtn[1];
ht1 = new Hashtable();
ht1.Add("ContractNumber", GenerateNo);
//GenerateNo is the method to generate number
relativeDestinationUrl = destinationDocuments.RootFolder.Url + "/" + NameofDocument;
destinationFile = ((SPDocumentLibrary)destinationDocuments).RootFolder.Files.Add(relativeDestinationUrl,fileBytes, ht1,OverwriteDestinationFile);
destinationFile.CheckIn("New Contract", SPCheckinType.MajorCheckIn);
}
catch (Exception ex)
{
//System.Diagnostics.EventLog.WriteEntry("Contract Details Exception", ex.Message);
}
finally
{
mySite.Dispose();
}
}

How to upload document into document library using C#.Net

String Continent = clistitem["Continent"].ToString();
fStream = FileUpload1.PostedFile.InputStream;
fStream.Read(contents, 0, (int)fStream.Length);

fStream.Close();

//storing the contents of the uploaded document
Bytes[] contents= new byte[fStream.Length];
fileName = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf('\\') + 1);

Hashtable ht = new Hashtable();
ht.Add("Continent", Continent);
SPFile fff = files.Add(fileName, contents, ht, true);
myweb.AllowUnsafeUpdates = true;
myweb.Update();

How to loop through all the Groups

foreach (SPGroup grp in myweb.Groups)
{
if (!grp.Name.Contains("Admin"))
{
foreach (SPUser user in grp.Users)
{
lbUsers.Items.Add(user.Name);
}
}
}

Saturday, November 22, 2008

How to find one list item from the Listitems

SPDocumentLibrary ContractsLib;
SPListItemCollection contractsItems;
ContractsLib = (SPDocumentLibrary)myweb.Lists["Contracts"];
SPListItem clistitem;
String EmployeeNumber=Request.QueryString["EmployeeNumber "] != null)
{
SPListItemCollection contractsItems;
String strContractNumber = Request.QueryString["ContractNumber"];
ENumberQuery = new SPQuery();
// Belowed one line of code will help you to loop through all the items;
ENumberQuery.ViewAttributes = "Scope=\"Recursive\"";
ENumberQuery.Query = "" +
" + "\"ID\"" + " />" +
"" +"" +"" +" + "\" EmployeeNumber \"" + " />" +
" + "\"Text\"" + ">" + strContractNumber + "" +
"" +
"";
contractsItems = ContractsLib.GetItems(ENumberQuery);

clistitem = contractsItems[0];


Friday, November 21, 2008

How to connect MOSS(Sharepoint Server 2007) using c#.Net

Add Microsoft.Sharepoint.dll as a reference in your Project.
Use the following namespace for your Project to utilise the Sharepoint classes, properties and methods.

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

Once you add the reference and namespace properly, intellisense will show you all the classes and methods properly.

mySite = new SPSite("http://Test-Sample:123/"); //You have to specify your site here.
SPWeb myweb = mySite.AllWebs["IT"]; //You have to speify the subsite here.
myweb.AllowUnsafeUpdates = true; //It will allow you to update listitem or List or anything from your code.
myweb.Update(); //It will update the web.

This code will be hardcoded. If you don't want to hardcode you can use the following code snippet

string Subsite = Request.QueryString["Subsite"]
mySite = new SPSite(this.Page.Request.Url.ToString());
SPWeb myweb = mySite.AllWebs[Subsite];
myweb.AllowUnsafeUpdates = true;
myweb.Update();

The above example is a good practice to implement it to your Project. In this project instead of subsite hardcoding, i used querystring to fetch the subsite from other forms. Based on the subsite value it will connect to your subsite.