Tuesday, 22 July 2014

Try Catch easy way Exception alerts

try{do some thing}
catch (Exception ex)
            {

                Response.Write("<script>alert(\"Failed to announcement Please contact to administrator\")</script>");

               // MessageBox.Show("Failed to announcement Please contact to administrator");

            }

Saturday, 19 July 2014

Simple way to upload the document to library

main syntax:  
1.folder creating 
2.items add to parent folder only.



    protected void btnsubmit_Click(object sender, EventArgs e)
        {

            #region maincode

            string strIssueID = txttitle.Text.Trim() + "_Announcement";
            // strIssueID = hdnIssueID.Value;
            //  Insert Issue details to List                            

            {

                SPWeb objWeb = SPContext.Current.Web;
                //SPList DocList = objWeb.Lists.TryGetList(this.parentwebpart.FeedbackDocList);
                SPList DocList = objWeb.Lists.TryGetList("ComAnnouncement");
               

                // Britannia.SP.Core.ULSLogger.LogTrace("Got List : " + DocList.Title, "Organoleptics - InsertDocToLibrary");
                if (DocList != null)
                {
                    // Britannia.SP.Core.ULSLogger.LogTrace("List Not Null", "Organoleptics - InsertDocToLibrary");

                    // Create folder in Document Library if not exists
                    if (DocList.ParentWeb.GetFolder(strIssueID).Exists)
                    {

                    }
                    else
                    {
                        SPListItem newfolder = DocList.Items.Add(DocList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strIssueID);
                     
                        newfolder["Modified By"] = SPContext.Current.Web.CurrentUser;

                        newfolder["Title"] = txttitle.ToString();
                        newfolder["Body"] = txtbody.ToString();
                        newfolder["Expires"] = calexpire.SelectedDate;
                   
                        newfolder["AnnouncementTitle"] = FileUpload1.PostedFile.FileName;

                        newfolder.Update();


                        objWeb.AllowUnsafeUpdates = true;

                       



                        if (DocList.ParentWeb.GetFolder(objWeb.Url + "/" + DocList.Title + "/" + strIssueID).Exists)
                        {


                            SPFolder IssueFolder = DocList.ParentWeb.GetFolder(objWeb.Url + "/" + DocList.Title + "/" + strIssueID);



                            SPFolderCollection folders = objWeb.GetFolder(DocList.RootFolder.ServerRelativeUrl).SubFolders;
                            foreach (SPFolder spfColl in folders)
                            {
                                //Since we have to create the SubFolder under MainFolder so we do this check.
                                if (spfColl.Name.Equals(newfolder.DisplayName))
                                {

                                    //obul comment  27/12/03
                                    spfColl.Files.Add(this.FileUpload1.PostedFile.FileName, this.FileUpload1.PostedFile.InputStream);

                                    spfColl.Update();
        }
                            }
                        }
                    }
                }
            }
        }



                                 
                                                                                   
                       




Method1:

protected void btnsubmit_Click(object sender, EventArgs e)
        {
         
            SPSite site = SPContext.Current.Site;              
            SPWeb web = site.OpenWeb();
            SPList list = web.Lists["ComAnnouncement"];              
            web.AllowUnsafeUpdates = true;              
            SPListItemCollection coll = list.Items;
            SPListItem newItem = coll.Add();

            Stream fStream = FileUpload1.PostedFile.InputStream;
            byte[] _byteArray = new byte[fStream.Length];
            fStream.Read(_byteArray, 0, (int)fStream.Length);
            fStream.Close();

            web.AllowUnsafeUpdates = true;

            string _fileUrl = list.RootFolder.Url + "/" + FileUpload1.PostedFile.FileName;

            bool IsOverwriteFile = true;
            SPFile file = list.RootFolder.Files.Add(_fileUrl, _byteArray, IsOverwriteFile);



     
                newItem["AnnouncementTitle"] = txttitle.ToString();
                newItem["Body"] = txtbody.ToString();
                newItem["Expires"] = calexpire.SelectedDate;



                //SPListItem newItem2 = coll.Add();
                newItem = file.Item;
                //SPListItem item = file.Item;
                newItem["Title"] = FileUpload1.PostedFile.FileName;

                newItem.Update();  
                //newItem2.Update();
                file.Update();
                //newItem["Title"] = txttitle.ToString();


             

                txttitle.Text = string.Empty;
                txtbody.Text = string.Empty;
                string seldt = calexpire.SelectedDate.ToString();
             
             
        }


Method2:

 Stream fStream = FileUpload1.PostedFile.InputStream;
            byte[] _byteArray = new byte[fStream.Length];
            fStream.Read(_byteArray, 0, (int)fStream.Length);
            fStream.Close();
            web.AllowUnsafeUpdates = true;
            string _fileUrl = list.RootFolder.Url + "/" + Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + DateTime.Now.ToString("MMddyyyyHHmmss") + Path.GetExtension(FileUpload1.PostedFile.FileName);
            bool IsOverwriteFile = false;
            SPFile file = list.RootFolder.Files.Add(_fileUrl, _byteArray, IsOverwriteFile);

            newItem = file.Item;


Method3:

            Stream fStream = FileUpload1.PostedFile.InputStream;
            byte[] _byteArray = new byte[fStream.Length];
            fStream.Read(_byteArray, 0, (int)fStream.Length);
            fStream.Close();

            web.AllowUnsafeUpdates = true;

            string _fileUrl = list.RootFolder.Url + "/" + FileUpload1.PostedFile.FileName;

            bool IsOverwriteFile = true;
            SPFile file = list.RootFolder.Files.Add(_fileUrl, _byteArray, IsOverwriteFile);
            newItem = file.Item;
            file.Update();

Wednesday, 16 July 2014

Removing "Download a Copy" context menu from a Document Library in MOSS2010

1).

 SharePoint 2007 (MOSS/WSS) - how to remove "Download a Copy" context menu from a Document Library

http://blogs.msdn.com/b/pranab/archive/2008/12/06/sharepoint-2007-moss-wss-how-to-remove-download-a-copy-context-menu-from-a-document-library.aspx



SharePoint 2010:

Here is what I did:
1. Added a Content Editor webpart to the AllItems.aspx page
2. Added following HTML into the HTML source of CEWP.

<style>
A[id$='Ribbon.Documents.Copies.Download-Large'] {
    DISPLAY: none
}</style>



2).Removing "Download a Copy" 

How to hide the download a copy option (Context Menu) in the Document Library

In the document library we have an option of downloading the document as shown below.
If the user wants to hides or remove this option for all the users. The following steps will explain the same.
1. First locate the following path C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033
2. Find the file named “CORE.js”. This is the file which will run the scripts downloading option for the document library.
3. Open the core.js file in best .js file editor and search for the function 
        AddSendSubMenu(m,ctx)
        Where you can see
        menuOption=CAMOpt(sm, L_DownloadACopy_Text, strAction, "");
        menuOption.id="ID_DownloadACopy";
These lines of code which will display text "Download A Copy" and call the menu operation.
So, comment the code and save the file.
4. Now when you search the context menu (Download a copy) option will be hidden as shown in the following figure.
Disadvantage
The above solution will affect all the document libraries of the SharePoint Server.
So, how to do it a particular document library or one web collection?
Hence the solution is as follows:
1. Open the default.master page in the designer.
2. Locate the core.js in the master page
<SharePoint:ScriptLink language="javascript" name="core.js"                  Defer="true" runat="server"/>
3.   So we can create our own (with the modifications as shown above) .js file (example named customcore.js) and save it in the following location
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033
4.   Now we can add the reference as shown below.
<SharePoint:ScriptLink language="javascript" name="customcore.js" Defer="true" runat="server"/>
5.   Now it won’t affect all the document libraries in the SharePoint server.
Bingo. We are done. We have restricted users to download the documents from the SharePoint Document library.