何小碩's profileGet More... ExperiencePhotosBlogListsMore ![]() | Help |
Get More... Experience我的知識庫 把所有在客戶端遇到的問題 所找到的資料統一蒐集在部落格 方便我後續的查閱 也藉此來分享網路上與 MOSS 有關的資訊 |
|||||||||||||
|
September 22 WebPart Connectable LifeCycleThis post describes the lifecycle events of a Sharepoint Connectable WebPart wih a single ViewState backed property; On Page Load
On 1st Postback |
|||||||||||||
| Posted on 19 July 2007 @ 20:07 in #MOSS 2007 If you need to upload a file into a SharePoint document library through code you can get started with this MSDN article: How to: Upload a File to a SharePoint Site from a Local Folder. In case you need to do the upload the file as an attachment to a custom list using the object model, the approach is slightly different. Adding a file to the list item can be done by accessing the Attachments collection of the SPListIem: //code snippet SPList list = web.Lists[new Guid("my list id")]; if (list != null) { web.AllowUnsafeUpdates = true; SPListItem item = list.Items.Add(); item["Title"] = "my title"; if (fileAttachment.PostedFile != null && fileAttachment.HasFile) byte[] contents = new byte[fStream.Length]; SPAttachmentCollection attachments = item.Attachments; } item.Update(); } //snippet end |
Posted by: Asfar Sadewa on: February 23, 2008
Well, not exactly removing, just crippling it so it won’t function. Hahah…
It all began with a guy in another department who got this persistent demand of getting rid of the single file upload button from a SharePoint document library upload menu. “We can upload single file from the multiple one, no need to have a single one.” He claimed. Hahah..
Given that I am a world-class-lazy-bum, I googled, but found that some working solutions applied globally towards the entire site collection, and not strictly to one doclib inside one site. Some claimed they have managed to do so by doing various tricks. But again, due to my acute laziness, I decided not to play around with Features, or even those object models, instead, I resorted to JavaScript.
What I did was described in Lazy 101; detect the Upload Menu by it’s many possible identifier (in this case i use its’ “text” attribute), and brutally disabled it. I cunningly inserted a content editor webpart in the page where the doclib tool bar was visible, and chuckled in an insane glee. Mwhuahahahhahuahahahah …
The script is as below:
<script type="text/javascript"> function GetElementByText(tagName, title) { var a = document.getElementsByTagName(tagName); for (var i=0; i < a.length; i++) { if (a[i].text) { if (a[i].text === title) { return a[i]; } } } return null; } if (window.onload) { var oLoad = window.onload; window.onload = function bodyLoad() { oLoad(); var o = GetElementByText("ie:menuitem","Upload Document"); if (o) { o.disabled = true; } } } </script>
[Update] Hey, guess what, a very kind guy has created a better tool for this (he’s even “featurize” it), check it here
<a href="disp.aspx?ID={@ID}">More...</a>Attachments: <SharePoint:AttachmentsField ControlMode="Display"
FieldName="Attachments" runat="server" Visible="true"/>
[@ID=$ListItemID]
If you’ve been following my blog you know that a bit more than a year ago I’ve published the Imtech Random Image Web Part: a SharePoint web part which allows you to display random images from a list of your choice. The web part I’ve made contains some code which you would have to deploy it in your SharePoint environment to get it working. Did you actually know that you can create the same functionality using no more than a couple of lines of JavaScript and no server-side code at all?
There are two things we need to display random images. First of all an image library with some images in it. It doesn’t matter how many or what kind of images you put in there: it’s okay as long as they are viewable in the browser.
The second thing we need is a Content Editor Web Part which we will use to include the JavaScript which will do the magic for us.
For the purpose of this case I have created a publishing site. The template you choose doesn’t really matter. A WSS3.0 Blank Site would do as well. I’ve uploaded some random photos I’ve found on Flickr to illustrate how it all works.
As soon as the images list is done, let’s proceed to the page.
Let’s start off with putting on the page the List View Web Part of the Images list:
Nothing special here. Now let’s do the magic. Let’s add a Content Editor Web Part, edit it, and paste in the Source Editor the following piece of HTML:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"], {
random: function(a, i, m, r) {
if (i == 0) {
jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
};
return i == jQuery.jQueryRandom;
}
});
</script>
<script type="text/javascript">
$().ready(function() {
var a = $("table.ms-summarystandardbody td.ms-vb2 a:random");
a.parents("div[id^=WebPart]").html('<img src="' + a.attr("href") + '"/>');
});
</script>
What it does? First of all I extend the jQuery framework with the “:random” filter which allows me to pick a randomly chosen element. Then, as soon as the page has been loaded I select all the links from the Images Web Part and pick a random one to display. The last thing I do is to substitute the list of images with the random image I’ve just picked:
That’s all!
Each time you load this page another image will be picked and displayed.
The example I’ve presented is quite simple. Because we’re using a common SharePoint list here, you could store some metadata together with the images and make them appear on the page as well. You could extend the script to add the title, description and maybe even a link to a different site: the sky is the limit.
Providing custom functionality doesn’t necessarily have to mean development and deployment. By just extending the standard SharePoint functionality with JavaScript you can easily create great features which can enhance the overall experience of your application. Using the jQuery framework you can simplify creating these enhancements even more. jQuery covers for you all the basics and allows you to focus on the functionality you to provide to your users.
ref:http://blog.mastykarz.nl/showing-random-images-sharepoint-2007-jquery/
Quote
Sharepoint Marquee Scroll Listings
One of our clients approached us with an interesting idea. I welcomed his request because he was such an ambitious client. Anyway, the request was scrolling the listings of any area. He wanted to display the listings on a marquee tag. While researching it, I couldn't find anything similar on the net, so I thought I’d share this webpart with you .
I created a C# webpart to have the following features :
1- display area listings ordered by the created date (last created first shows)
2- an icon image (e.g. Company logo) separates between listings
Since I have attached the source code of this webpart; you can upgrade the solution to have other feature like:
1- show the top (n) number of listings
2- stop scrolling when mouse over
3- you can set a scroll left/scroll right functions
Here are simple steps to deploy the webpart on your portal
Step 1
Download the the project from here http://cid-c6d240d97b073074.skydrive.live.com/self.aspx/Attachments/MarqueeListings_Zip.zip
Step 2
Go to the bin\Debug where the assembly file of the Marquee listing resides(MarqueeListings.dll) .this dll file has to be dragged and dropped to the GAC
(HD:\WINDOWS\assembly ).
Step 3
Copy the image MarqueeIcon.GIF to the sharepoint images
location : HD:\Program Files\Common Files\Microsoft Shared\web server
extensions\60\TEMPLATE\LAYOUTS\1033\IMAGES
Step 4
The webpart has to be registered as safe control. so copy the following script
<SafeControl Assembly="MarqueeListings, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=78fabca6851eb8f0" Namespace="Sharepoint.Afkar.MarqueeListings" TypeName="*" Safe="True" />
< P Assembly="MarqueeListings, Version=1.0.0.0, Culture=Neutral,
Then add it to the web.config which resides on the virtual directory of your portal
Step 5
Restart IIS.
Step 6
Import the webpart MarqueeGroupedListings.dwp to the portal. The web part displays the grouped listings of the home page of the server . if you want to change to display the listings of another area you need to modify the web part .under the Marquee options write the Url of the area (e.g. http://serverName/Area2)
Step 7
There is no step 7. Its Over!!! But incase you want to display other area's listings you need to edit the webpart properties giving the correct area URL
Code Sample
private const string defaultURL = "";
private string _URL = defaultURL;[Browsable(true),Category("Marquee Options"),
DefaultValue(defaultURL),
WebPartStorage(Storage.Personal),
FriendlyName("Area URL"),Description("Area URL")]
public string URL
{
get
{
return _URL;
}set
{
_URL = value;
}
}
protected override void RenderWebPart(HtmlTextWriter output)
{
try
{
//******************************This section to get the Grouped listings of any area***************
AreaListingCollection areaListings=GetAreaListings(this.URL.ToString());
if (areaListings!=null)
{
if (areaListings.Count>0)
{
string str="";
int counter=0;
ArrayList textArrayList = new ArrayList();
ArrayList linkArrayList = new ArrayList();
//this is order the last one entered appears in the first
int TotalNewNUm=0;foreach (AreaListing areaListing in areaListings)
{
textArrayList.Add (areaListing.Title.ToString() ) ;
linkArrayList.Add (areaListing.URL.ToString() ) ;
TotalNewNUm++;
}//foreach
int i ;
for(i=TotalNewNUm-1 ;i>=0 ;i--)
{
str+= "<a href=" + linkArrayList[i] + " target=_blank > <span style='font-family:verdana ; font-size: 8pt ; height:5px; margin-bottom:3px; '>" + textArrayList[i] + "</span> </a>" ;
counter++;
if (i !=0)
{
str+= "<img style='margin-top:5px;' src='/_layouts/1033/images/MarqueeIcon.GIF' >" ;
}
}output.Write( " <marquee>" + str + "</marquee>" );
}
else
{
output.WriteLine (" There is No listings in this area " + this.URL.ToString() + " !");
}
}
else
{
output.Write ("Wrong area URL, Go back to the webpart properties to correct the area URL (e.g http://serverName:port/areaName)");
}}
catch (Exception ee)
{
output.Write ( "Errorr : " + ee.Message );
}
}
private AreaListingCollection GetAreaListings(string url)
{SPSite siteCollection;
SPWeb site ;try
{
if (url=="")
{
siteCollection = SPControl.GetContextSite(Context);
site = SPControl.GetContextWeb(Context);
}
else
{
siteCollection = new SPSite(url);
site = siteCollection.OpenWeb();
}
TopologyManager topologyManager = new TopologyManager();
Uri uri =new Uri(siteCollection.Url);//new Uri("http://MyPortal");
PortalSiteCollection sites = topologyManager.PortalSites;
PortalContext portalContext = PortalApplication.GetContext(sites[uri]);
Area newsArea = AreaManager.GetArea(portalContext, site.ID);
return newsArea.Listings;
}
catch (Exception ee)
{
return null;
}
}
This Article Published on:
http://ahmadadainat.spaces.live.com
Please drop me an email if you have any comments or enquiries. ahmad.adainat@gmail.com , dont forget to swing by my blog to see my latests.
| |||||
|
|