Showing posts with label upload. Show all posts
Showing posts with label upload. Show all posts

Wednesday, 27 November 2013

programmatically upload document library file in SharePoint 2013 with meta data using c#

string fileNameonly = Fileupload1.FileName;  // Only file name.

 private byte[] ToByteArray(Stream inputStream)
        {
            using (MemoryStream ms = new MemoryStream())
            {

                inputStream.CopyTo(ms);
                return ms.ToArray();
            }

        }

private void AddFileToDocumentLibrary(string documentLibraryUrl, string filename, string Title)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(documentLibraryUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        Stream StreamImage = null;
                        if (Fileupload1.HasFile)
                        {
                            StreamImage = Fileupload1.PostedFile.InputStream;
                        }
                        byte[] file_bytes = ToByteArray(StreamImage);
                        web.AllowUnsafeUpdates = true;
                        SPDocumentLibrary documentLibrary = (SPDocumentLibrary)web.Lists["DocumentLibraryName"];
                        SPFileCollection files = documentLibrary.RootFolder.Files;
                        SPFile newFile = files.Add(documentLibrary.RootFolder.Url + "/" + filename, file_bytes, true);
                        SPList documentLibraryAsList = web.Lists["DocumentLibraryName"];
                        SPListItem itemJustAdded = documentLibraryAsList.GetItemById(newFile.ListItemAllFields.ID);
                        SPContentType documentContentType = documentLibraryAsList.ContentTypes["Document"]; //amend with your document-derived custom Content Type
                        itemJustAdded["ContentTypeId"] = documentContentType.Id;
                        itemJustAdded["Title"] = Title;
                        itemJustAdded.Update();
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });
        }
Continue Reading...

Thursday, 21 November 2013

sharepoint custom attachement upload control with example

                        sharepoint custom attachement upload control with example 

Design Code:-

<table> <tr><td>
<asp:HiddenField Value="hDeleteAttachs" ID="hHiddenFields" runat="server" />
</td></tr>
<tr>
<td><span id="part1">
<SharePoint:AttachmentButton runat="server" ID="btnAttach" ControlMode="new" Text="Add Attachment">
</SharePoint:AttachmentButton> </span></td></tr>
<tr><td id='idAttachmentsRow'>
<SharePoint:AttachmentUpload runat="server" ID="uploadAttach" ControlMode="New">
</SharePoint:AttachmentUpload></td></tr>

<tr><td><SharePoint:AttachmentsField runat="server" ID="fieldAttach" ControlMode="new" FieldName="Attachments">
</SharePoint:AttachmentsField></td></tr>
</table>
<asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submitButton_Click" />
Cs Code:-

SPSite Objsite = null;
SPWeb Objweb = null;
SPSite noprevsite = null;
SPWeb noprevweb = null;
SPList Olist = null;
SPListItem Osplistitem = null;
protected void Page_Load(object sender, EventArgs e)
{
noprevsite = SPContext.Current.Site;
noprevweb = noprevsite.OpenWeb();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
Objsite = new SPSite(noprevsite.ID);
Objweb = Objsite.OpenWeb(noprevweb.ID);
Objweb.AllowUnsafeUpdates = true;
Olist = Objweb.Lists["Users"];
btnAttach.ListId = Olist.ID;
uploadAttach.ListId = Olist.ID;
fieldAttach.ListId = Olist.ID;
Osplistitem = Olist.Items.Add();
});
}
protected void submitButton_Click(object sender, EventArgs e)
{

SPWeb mySite = SPContext.Current.Web;
SPList myList = mySite.Lists["Users"];
SPListItem myListItem = myList.Items.Add();
myListItem["Title"] = "myTitle";
if (HttpContext.Current.Request.Files.Count > 0)
{
HttpFileCollection uploads = HttpContext.Current.Request.Files;
SPAttachmentCollection attachments;
for (int i = 0; i < uploads.Count; i++)
{
Stream fs = uploads[i].InputStream;
byte[] fileContents = new byte[fs.Length];
fs.Read(fileContents, 0, (int)fs.Length);
fs.Close();
attachments = myListItem.Attachments;
string fileName = Path.GetFileName(uploads[i].FileName);
attachments.Add(fileName, fileContents);
}
myListItem.Update();
}
}

Continue Reading...

Followers

Follow The Author